変数の型を調べる


■動作ブラウザ 【 IE:3.0  NN:3.0】
Internet Explorer Netscape Navigator DreamPassport iCab
3.0x 4.0x 4.5 5.0x 5.5 2.0x 3.0x 4.0x 4.x 6.0 2 3 2.x
Windows ○ ○ - ○ ○ × ○ ○ ○ ○ - ○ -
Macintosh ○ ○ ○ ○ - × ○ ○ ○ ○ - - ○
UNIX - - - - - × ○ ○ ○ ○ - - -
Dreamcast - - - - - - - - - - ○ ○ -

ポイント str = typeof(n);
説  明 変数の型を調べるには「typeof(変数名)」のように記述します。戻り値は文字列であればstring、数値であればnumber、オブジェクトであればobjectとなります。
サンプル <html> <head> <title>変数の型を調べる</title> </head> <body> <script language="JavaScript"><!-- n = 12; str = typeof(n); document.write("n : ",str,"<br>"); n = "12"; str = typeof(n); document.write("n : ",str,"<br>"); n = document; str = typeof(n); document.write("n : ",str,"<br>"); // --></script> </body> </html>
補足説明 なし

■サンプルスクリプトを実行する >>実行
■各ブラウザでの動作結果を見る >>View!

写真素材 PIXTA