0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScriptにおける演算子の実行順

Posted at

今までなんとなくで使っていましたが、改めて調べたのでメモとして。

まず先に優先順で処理し、同じ優先順の中では結合順に従って処理していくようです。
結合順という概念を今回調べて初めて知りました。

// ①
console.log(1 + 2 * 3)
// => 7
// 優先順位が異なる(掛け算が優先)ので、記述順に関わらず2*3が先に実行される

// ②
console.log(2 * 2 ** 3)
// => 16
// 優先順位が異なる(べき乗が優先)ので、記述順に関わらず2**3が先に実行される

// ③
console.log(2 ** 3 ** 2)
// => 512
// 優先順位は同じだが、算術演算子の中でべき乗は唯一`右結合`するので、右側の計算(3**2)が先に実行される

// ④
let a
let b
a = b = 3
console.log(a)
// = > 3
// 代入演算子も`右結合`するので、b = 3, a = bの順に実行されることからaはundefinedにならない

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?