用途
新着
履歴
分類

JS 値の型について

JS 値の型について
代表的な値の型について、型を検出するtypeof関数を使って表示したサンプルを紹介します。

変数の中にはいろんな型があって型によって処理を分けないと思った通りに動いてくれないことがあります。

typeof関数を使うと型を調べることができます。

object

JS

var obj = { id: 1 };
document.getElementById('demo_obj').textContent = (typeof obj);

HTML

 = <span class="demo_value" id="demo_obj">

上記の結果

=

array

JS

var arr = [100, 'abc', 300];
document.getElementById('demo_arr').textContent = (typeof arr);

HTML

 = <span class="demo_value" id="demo_arr">

上記の結果

=

配列も objectとなるので配列の判定はArray.isArray()で行うと良いらしいです。


string

JS

var str = 'abc';
document.getElementById('demo_str').textContent = (typeof str);

HTML

 = <span class="demo_value" id="demo_str"></span>

上記の結果

=

number

JS

var num = 123;
document.getElementById('demo_num').textContent = (typeof num);

HTML

 = <span class="demo_value" id="demo_num"></span>

上記の結果

=

boolean

JS

var boo = true;
document.getElementById('demo_boo').textContent = (typeof boo);

HTML

 = <span class="demo_value" id="demo_boo"></span>

上記の結果

=

function

JS

var func = function hoge() {};
document.getElementById('demo_func').textContent = (typeof func);
 = <span class="demo_value" id="demo_func"></span>

上記の結果

=
公開 2020-01-19 23:29:07
更新 2020-01-21 13:38:37
このページの二次元コード
JS 値の型について

人気のサンプル

search -  category -  about
© 2024 kipure
Top