js判断变量是否为字符串的方法
在javascript脚本代码想要判断某个变量是否为字符串,有多种判断方法。下面小编就详细的说一下。
使用typeof操作符来判断数据类型,如果为string则为字符串。
let str = 'https://www.73so.com';if(typeof str === 'string'){console.log('为字符串');}else{console.log('不是字符串');}
例2:
可以使用正则表达式来判断数据是否为字符串。
let str = 'abcd';if (/^[a-zA-Z]+$/.test(str)) {console.log("数据为字符串");} else {console.log("数据为数组");}
以上就是js中两种判断数据是否为字符串的方法,当前其它的方法也很多,大家只做个参考即可。
可通过以下几种方式联系或关注我们!
联系我们
可通过以下几种方式联系或关注我们!
Q Q:540344537
微信:mowu1086
链接:
关注我们

本文地址: https://www.earthnavs.com/jishuwz/26819468813b139fb899.html