変数の中にはいろんな型があって型によって処理を分けないと思った通りに動いてくれないことがあります。
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>上記の結果
=
 
		




 
									 
									 
									 
									 
									