0
0

More than 1 year has passed since last update.

typeof と instanceof

Posted at

変数 s が文字列かどうかを確認するコードで、

typeof s === 'string' || s instanceof String

というものを見かけた。

名前の通り、あるクラスをインスタンス化してできたものは instanceof で判別、それ以外で作られたものは typeof で判別?

str1 = new String('str1')
str2 = 'str2'

console.log(typeof str1) // object
console.log(typeof str2) // string
console.log(str1 instanceof String) // true
console.log(str2 instanceof String) // false

読んだ記事

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