es7

ES7 (ECMAScript 2016)

Array.prototype.includes

Array .includes() determines if an array includes an specified element, returning true if it includes, false otherwise.

Syntax

includes :: (searchElement, ?fromIndex) -> Boolean

Parameters

  • searchElement – the element to search on the array.
  • fromIndex – the index from which the search starts.

Examples

includes :: (searchElement) -> Boolean

[].includes()                   // false
[].includes(undefined)          // false
['foo',3,{}].includes({})       // false
['foo','bar'].includes('zed')   // false

[-0].includes(+0)               // true
[1,2,3].includes(2)             // true
['foo','bar'].includes('foo')   // true
[undefined].includes(undefined) // true

includes :: (searchElement, fromIndex >= 0) -> Boolean

['foo','bar','zed'].includes('foo')    // true - fromIndex 0 by default
['foo','bar','zed'].includes('foo',1)  // false
['foo','bar','zed'].includes('bar',1)  // true
// If the fromIndex is greater or equal to the array size,
// the array is not searched and false is returned.
['foo','bar','zed'].includes('bar',10) // false

includes :: (searchElement, fromIndex < 0) -> Boolean

// When fromIndex is negative a computed index is calculated to be used.
// If the computed index if less than 0, the entire array is search.
// The computation goes like this:
// Computed index = Array length + fromIndex
// e.g.
var array = ['a','b','c','d'] // length = 4
array.includes('b', -3)  // computed index = 4 + (-3) = 1,   result = true
array.includes('b', -1)  // computed index = 4 + (-1) = 3,   result = false
array.includes('b', -10) // computed index = 4 + (-10) = -6, result = false

Further Reading

Array.prototype.includes (Domenic Denicola, Rick Waldron)

Exponentiation Operator

Exponentiation operator returns the result of raising the first operand to the power of the second (e.g. x ** y).
It will return the same result as Math.pow(x,y).

Syntax

x ** y

Example

// x ** y (aka Math.pow(x,y))
2 ** 2   // 4
2 ** 'a' // NaN

var e = 2
e **= 2  // 4

Further Reading

Exponentiation Operator (Rick Waldron)

Thanks to 🍻

Visit original content creator repository

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *