2
3

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

ここでは

var name = '鈴木一郎';
document.write(name);

を使って変数の取り扱いについて進めてみる。

重要なのは
var name = '鈴木一郎';
で設定した変数name''""でくくらない。
↑(文字列として認識してnameと出力されてしまう)

うまくやれば、下のようになる。
変数01.png

varを使って箱を作ることを「変数の定義」、=を使って箱にデータを入れることを「代入」と言います。
また、変数を定義してデーターを代入することを「変数にデータを保存」するとも言う。

変数に代入出来るデータの種類

以下5タイプに分類される。

var str = 'string';         //文字列
var num = 100;              //数値
var arr = ['a', 'b'];       //配列
var obj = { a: 'b' };       //オブジェクト
var fun = function() {};    //関数

変数の値の更新

var name = '春日歩';
name = 'おおさか';
document.write(name);

上記の様にすると最終的には

変数02.png

「春日歩→おおさか」へ変更(更新)されてる。

2
3
2

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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?