1. 首页
  2. 软件开发
  3. JavaScript 中如何检测一个变量是一个 String 类型?

JavaScript 中如何检测一个变量是一个 String 类型?

  • 发布于 2024-09-26
  • 1 次阅读

三种方法(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


原文链接: https://blog.csdn.net/hexadecimal_001/article/details/139681007