This article introduces you to the latest version of JavaScript: ECMAScript 2016 (also known as ES7). ES7 says that the changes I’ve made are minimal and consist of two minor features: array.prototype.includes () and the exponential operator: **


Array.prototype.includes()


Gone are the days when we needed.indexof () to determine if an element was in an array.


['my','mom','hates','me'].indexOf('mom')  // 1

// I know it sounds confusing but the value 1 represents 

// the index at which the string 'mom' appears in the array.

// Not that I only have 1 mom.


Notice that the keyword is ‘exist.’


If we want to know the index value of a given element in an array,.indexof () is perfectly OK.


But if our goal is to know whether a given element exists in an array, it is clear that.indexof () is not a good option. The reason is simple: but when we query for the existence of something, we expect the result to be a Boolean, not a number.


Array.prototype.includes() It determines whether a given element exists in an array and returns true if it does, flase otherwise.


var life = ['mom', 'dad', 'brother']

life.includes('mom')          // true

life.includes('girlfriend')   // false


specification


Array.prototype.includes ( searchElement [ , fromIndex ] )
  • SerchElement – The element to find
  • FromIndex (Optional) – Index value for the location to start the search

Looking at specifications is like looking for power


What does the specification say?


Take your time, let’s take it step by step, and then use examples to understand the specifications.



# includes

# [1] searchs in ascending order

> Array(10000000).concat(4).includes(4)

true # [Time] 500 miliseconds

> [4].concat(Array(10000000)).includes(4)

true # [Time] 90 miliseconds


# [2] uses SameValueZero algorithm

> [NaN].indexOf(NaN)

- 1

> [NaN].includes(NaN)

true


# [3] if found at any position returns true

# otherwise, false is returned

> [1, 2, 3].includes(2)

true

> [1, 2, 3].includes(7)

false


# [4] it treats missing array elements as undefined

> [1, , 3].indexOf(undefined)

- 1

1. The difference here is the position of element ‘4’. In our first example we put 4 behind and includes will search the entire array. As specified,.includes() returns as soon as it finds the searchElement. So the second operation is faster.


2. The SameValueZero algorithm, compared to Strict Equality Comparison (used by.indexof ()), allows NAN elements to be found!


3. Return true when the element is found, false otherwise.


4. Unlike.indexof (),.includes() does not ignore missing elements in the array.


Do you feel this mysterious power?


Now let’s talk about the fromIndex


Let’s look at this specification:

The optional second parameter fromIndex defaults to 0; If greater than or equal to the length of the array, false is returned and the array will not be searched; If it is negative, fromIndex(Array Length + fromIndex) will be calculated from the end of the array. If the calculated index is less than 0, the entire array will be searched.

# fromIndex

# [1] it defaults to 0

> [1, 2, 3]. Includes (1)

true

# [2] if >= array.length, false is returned

> [1, 2, 3]. Includes (1, 10)

false

# [3] if negative, it is used as the offset, i.e.

# offset = array.length + fromIndex

> [1,2]. Includes (3, -2) # fromIndex = 4 (array length) + -fromindex = 1

true

# [4] if offset < 0, the whole array is searched

> [1,2,3]. Includes (1, -5) # fromIndex = 0


1. If no fromeIndex value is given, the default value 0 will be used and the entire array will be searched.


2. If fromIndex is larger than the array length,.includes() will immediately return false


3. If fromIndex is negative, a calculated array.length + fromIndex is given as its value. This is useful when the element you are looking for is at the end of the array. For example, fromIndex = -5 would be equivalent to searching the last five elements.


4. To avoid crashes in the.includes() method, the entire array is searched when the fromIndex value is less than 0.


Ok, one last feature…


Power operator (**)


We can finally exponentiate like addition, subtraction, multiplication and division


The ** operator is used in the same way as math.pow (), taking the first operand as the base and the second as a power (such as x ** y).

# x ** y (aka Math.pow(x,y))

> * 2 * 2

4

> 2 ** 'operand'

NaN


Well, that’s the end of today’s introduction, ES7’s capabilities have been delivered, let’s use them


via medium.freecodecamp.org