「JavaScript/演算子」の版間の差分

削除された内容 追加された内容
Ef3 (トーク | 投稿記録)
→‎演算子の優先順位: |- style="position:sticky; top:0"
Ef3 (トーク | 投稿記録)
paiza
タグ: 2017年版ソースエディター
49 行
'''加算演算子'''(かさんえんざんし、''addition operator'')は<code>+</code>です<ref>[https://tc39.es/ecma262/#sec-addition-operator-plus ECMA-262::13.8.1 The Addition Operator ( + )]</ref>。
 
;[https://paiza.io/projects/ySNs2SNCO4Eko8b0HPCE-g 変数twoを1+1で初期化するとtwoの値は2となる]:<syntaxhighlight lang="javascript">
const two = 1 + 1; // 変数twoを1+1で初期化するとtwoは2となる
alertconsole.log(two); // 2
</syntaxhighlight lang="javascript">
;実行結果:<syntaxhighlight lang="text">
2
</syntaxhighlight>
 
57 ⟶ 60行目:
'''減算演算子'''(げんざんえんざんし、げんさんえんざんし、''subtraction operator'')は<code>-</code>です<ref>[https://tc39.es/ecma262/#sec-subtraction-operator-minus ECMA-262::13.8.2 The Subtraction Operator ( - )]</ref>。
 
;[https://paiza.io/projects/wxzLEVD2-1eOuxIr9L3tXA?language=javascript 変数minus_oneを0-1で初期化するとminus_oneの値は-1となる]:<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
const minus_one = 0 - 1; // 変数minus_oneを0-1で初期化するとminus_oueは-1となる
alertconsole.log(minus_one); // -1
</syntaxhighlight>
;実行結果:<syntaxhighlight lang="text">
-1
</syntaxhighlight>
 
67 ⟶ 73行目:
<syntaxhighlight lang="javascript">
const four = 2 * 2; // 変数fourを2×2で初期化するとfourは4となる
alertconsole.log(four); // 4
</syntaxhighlight>
 
75 ⟶ 81行目:
<syntaxhighlight lang="javascript">
const one_half = 1 / 2; // 変数one_halfを1÷2で初期化するとone_halfは0.5となる
alertconsole.log(one_half); // 0.5
</syntaxhighlight>
 
82 ⟶ 88行目:
 
<syntaxhighlight lang="javascript">
alertconsole.log( 1 / 0 ); // Infinity
alertconsole.log( -2 / 0 ); // -Infinity
alertconsole.log( 3 /-0 ); // -Infinity
alertconsole.log( -3 /-0 ); // Infinity
alertconsole.log( 1n / 0n ); // RangeError: Division by zero
</syntaxhighlight>
 
93 ⟶ 99行目:
 
<syntaxhighlight lang="javascript">
alertconsole.log( 0 / 0 ); // NaN
alertconsole.log( 0n / 0n ); // RangeError: Division by zero
</syntaxhighlight>
 
102 ⟶ 108行目:
<syntaxhighlight lang="javascript">
const one = 10 % 3; // 10÷3は3余り1なので変数oneは1となる
alertconsole.log(one); // 1
</syntaxhighlight>
 
109 ⟶ 115行目:
<syntaxhighlight lang="javascript">
const minus_one = -10 % 3;
alertconsole.log(minus_one); // -1
</syntaxhighlight>
 
116 ⟶ 122行目:
 
<syntaxhighlight lang="javascript">
alertconsole.log( 10 % 0 ); // NaN
alertconsole.log( 10n % 0n ); // RangeError: Division by zero
</syntaxhighlight>
 
144 ⟶ 150行目:
<syntaxhighlight lang="javascript">
const theta = Math.PI / 4;
alertconsole.log(Math.sin(theta) === Math.sin(theta % (2 * Math.PI))); // true
</syntaxhighlight>
 
152 ⟶ 158行目:
<syntaxhighlight lang="javascript">
const two = "2";
alertconsole.log(+two); // 2
</syntaxhighlight>
 
183 ⟶ 189行目:
<syntaxhighlight lang="javascript">
const one = 1;
alertconsole.log(-one); // -1
</syntaxhighlight>
 
196 ⟶ 202行目:
let x = 0;
x++;
alertconsole.log(x); // 1
</syntaxhighlight>
 
204 ⟶ 210行目:
let x = 0;
const y = x++;
alertconsole.log(y); // 0
alertconsole.log(x); // 1
</syntaxhighlight>
 
213 ⟶ 219行目:
let x = 0;
const y = ++x;
alertconsole.log(y); // 1
alertconsole.log(x); // 1
</syntaxhighlight>
 
232 ⟶ 238行目:
let x = 0;
x--;
alertconsole.log(x); // -1
</syntaxhighlight>
 
240 ⟶ 246行目:
let x = 0;
const y = x--;
alertconsole.log(y); // 0
alertconsole.log(x); // -1
</syntaxhighlight>
 
249 ⟶ 255行目:
let x = 0;
const y = --x;
alertconsole.log(y); // -1
alertconsole.log(x); // -1
</syntaxhighlight>
 
267 ⟶ 273行目:
<syntaxhighlight lang="javascript">
const power = 2 ** 10;
alertconsole.log(power); // 1024
</syntaxhighlight>
なお、<code>^</code>はビットごとのXOR演算子です。
312 ⟶ 318行目:
<syntaxhighlight lang="javascript">
const str = "Wiki" + "books";
alertconsole.log(str); // "Wikibooks"
</syntaxhighlight>
 
319 ⟶ 325行目:
<syntaxhighlight lang="javascript">
const str = "JavaScript " + 1.5;
alertconsole.log(str); // "JavaScript 1.5"
</syntaxhighlight>
 
327 ⟶ 333行目:
const one = "1";
const two = one + 1;
alertconsole.log(two); // "11" -- alertconsole.log()メソッドではわかりにくいですが、2文字の文字列です。
</syntaxhighlight>
 
335 ⟶ 341行目:
const one = "1";
const two = parseInt(one) + 1;
alertconsole.log(two); // 2
</syntaxhighlight>
 
343 ⟶ 349行目:
const one = "1";
const two = +one + 1;
alertconsole.log(two); // 2
</syntaxhighlight>
 
439 ⟶ 445行目:
let x = 1;
x += 1; // x = x + 1
alertconsole.log(x); // 2
</syntaxhighlight>