JS篇(002)-JavaScript 中如何检测一个变量是一个 String 类型?

答案:三种方法(typeof、constructor、Object.prototype.toString.call())

解析:

①typeof

typeof('123') === "string" // true

typeof '123' === "string" // true


②constructor

'123'.constructor === String // true


③Object.prototype.toString.call()

Object.prototype.toString.call('123') === '[object String]' // true

参与互动

posted @ 2021-05-26 13:35  手掌日月摘星辰  阅读(408)  评论(0编辑  收藏  举报