0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaScript 型を調べる

Posted at

用途

  • JavaScriptのコードで型を調べたい時に使用する。

注意
typeof は「null」や「object」を正しく判別できない。
その場合は Object.prototype.toString.call() を使用する。

使用方法

typeof

書き方
typeof(型を調べたい値);
console.log(typeof("hogehoge"));
console.log(typeof(1));
console.log(typeof(null));
console.log(typeof([hoge1, hoge2, hoge3]));

// 結果
// string
// number
// object
// object

「null」と「配列」が object で返ってくる。

Object.prototype.toString.call()

書き方
var hoge = Object.prototype.toString;
console.log(hoge.call(型を調べたい値));
var hoge = Object.prototype.toString;
console.log(hoge.call("hogehoge"));
console.log(hoge.call(1));
console.log(hoge.call(null));
console.log(hoge.call([hoge1, hoge2, hoge3]));

// 結果
// string
// number
// null
// array

「null」と「配列」が nullarray で返ってくる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?