LoginSignup
0
0

includes()とべき乗

Posted at

ES2016(ES7)から実装された2つの機能をまとめる

TypedArray.prototype.includes()

配列内に指定した要素が含まれているかどうかを調べる。返り値はBoolean。
includesメソッドに第2引数を指定すると配列内で調べ始める位置をインデックス指定できる。また、負の整数を指定すると調べ始める位置を後ろから指定できる。

const array = ['cat', 33, 'dog', 91, 30, 'bird'];

array.includes('dog');     // true
array.includes('dog', 2);  // true
array.includes('dog', 3);  // false
array.includes(91, -3); // true
array.includes(91, -2); // false

べき乗計算(Exponentiation Operator)

べき乗計算はもともとMath.pow()で行なっていたが、新しく**でも行えるようになった。右結合なので、計算の順番に注意する。

優先度と結合性

Math.pow(5, 2) // 25
5 ** 2         // 25

(5 ** 2) ** 3) // 15625
5 ** 2 ** 3    // 390625

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0