ここでは
var name = '鈴木一郎';
document.write(name);
を使って変数の取り扱いについて進めてみる。
重要なのは
var name = '鈴木一郎';
で設定した変数name
は''
や""
でくくらない。
↑(文字列として認識してname
と出力されてしまう)
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);
上記の様にすると最終的には
「春日歩→おおさか」へ変更(更新)されてる。