(1)簡単な足し算
var result = 10 + 20;
document.write(result);
-------=> 30
(2)変数を利用した場合
var x = 10
var y = 20
var result = x + y;
document.write(result);
--------=> 30
(3)5つの四則演算
最初に3つの変数を定義
var x = 10;
var y = 4;
var result;
// 足し算
result = x + y;
document.write(result); //=> 14
// 引き算
result = x - y;
document.write(result); //=> 6
// かけ算
result = x * y;
document.write(result); //=> 40
// 割り算
result = x / y;
document.write(result); //=> 2.5
// 割り算の(あまり)
result = x % y;
document.write(result); //=> 2
計算の優先順位
var x = 10;
var y = 3;
var z = 5;
// かけ算の方が優先される
document.write(x + y * z); //=>25
// ()内が優先される
document.write((x + y) * z); //=>65
自分自信を演算する場合
var x = 10;
x = x + 5;
document.write(x);
---------=>15
インクリメント(増加)とデクリメント(減少)
自分自身に1足すには「x++」、1引くには「x--」と表記する。
var x = 10;
x++;
document.write(x);
---------=>11
数値と文字列の扱い方
文字列と数値の扱い違いについて
「+で連結した場合」で検証してみる。
① 数値の場合
var x = 10;
var y = 20;
var result = x + y;
document.write(result);
-------=> 30
② 文字列の場合
var x = '10';
var y = '20';
var result = x + y;
document.write(result);
-------=> 1020
③ 数値に文字列を結合した場合
var x = 10;
var y = '20';
var result = x + y;
document.write(result);
-------=> 1020 ↓