6
5

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 5 years have passed since last update.

四則演算

Last updated at Posted at 2014-01-30

(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 ↓

数値に文字列.png

6
5
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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?