変数とデータ型
変数
- 変数の宣言と代入
- var 変数名 = 値;
- 変数の出力
-
document.write('文字列' + 変数名 + '文字列);
※文字列の結合は+
コーテーションマーク
- ダブルコーテーションとシングルコーテーション
-
JavaScriptにおいては両者は差はない
ただhtmlはダブルコーテーションを多用するので、バッティングしにくいシングルコーテーションが好まれる。 - エスケープ
-
htmlのダブルコーテーションとJavaScriptのダブルコーテーションがバッティングするときにはhtmlのダブルコーテーションの前にエスケープ文字(\)を置く
JavaScriptがわでシングルコーテーションを使うときには問題ない
データの型
- 型変換
-
var str = '2';
var number1 = 2;
var number2 = 1;
number1 + number2は数値型(出力結果は3)
str + number1は文字列型(出力結果は21)
- 弱いデータ型
- JavaScriptでは変数のデータ型は自動的に変換されるので余り意識しなくても使える(Excelに近い)。
- 強いデータ型
- 変数がどのデータ型なのかをあらかじめ宣言して使用する必要がある言語もある(Accessに近い)。