- 在JS字符串中插入单引号或双引号(通过’'转义字符添加识别)
var str = "小明说:\"你好\""
console.log(str)
打印结果
2. 通常使用typeof 变量
检查数据类型
3. NaN https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NaN
4. 数据类型转换
(1) 转换为字符串
a. 调用toString()
方法(null与undefined不可用)(或者添加+ ""
)
var a = 123;
console.log(typeof a);
console.log(typeof a.toString());
console.log(a.toString())
b. 调用String()
函数(null与undefined可用)
var a = 123;
a = String(a);
console.log(typeof a);
(2) 转换为number(可通过-0,*1,/1转换)
a. 使用Number()
函数(只有数字能成功转换,非数字为NaN,空串为0,true转换为1,false为0,null为0,undefined为NaN)
var a = 123;
a = Number(a);
console.log(typeof a);
console.log(a)
b. 使用parseInt()
与parseFloat()
函数(提取字符串中有效数字内容,对非字符串内容先转换为字符串再操作)(可在函数中变量后添加数字表示转换进制)
var a = "123em";
a = parseInt(a);
console.log(typeof a);
console.log(a)
(3)转换为布尔值
数字转布尔值:除了0与NaN,其余都为true
字符串转布尔值:除了空串,其余都为true
null与undefined转换为false
使用Boolean()
函数
var a = "123em";
a = Boolean(a)
console.log(typeof a);
console.log(a)
隐式转换:https://blog.csdn.net/weixin_44176774/article/details/86498996