JavaScript 是弱类型语言,类型转换是自动进行的。理解类型转换规则对于避免 bug 非常重要。
String(123); // "123"
String(true); // "true"
String(null); // "null"
String(undefined); // "undefined"
(123).toString(); // "123"
true.toString(); // "true"
123 + ''; // "123"
true + ''; // "true"
Number('123'); // 123
Number('123abc'); // NaN
Number(true); // 1
Number(false); // 0
Number(null); // 0
Number(undefined); // NaN
parseInt('123'); // 123
parseInt('123abc'); // 123
parseInt('abc123'); // NaN
parseFloat('123.45'); // 123.45
+'123'; // 123
+true; // 1
+false; // 0
Boolean(1); // true
Boolean(0); // false
Boolean(''); // false
Boolean('0'); // true
Boolean(null); // false
Boolean(undefined); // false
Boolean([]); // true
Boolean({}); // true
!!1; // true
!!0; // false
!!''; // false
'1' + 2; // "12" (字符串拼接)
'1' - 2; // -1 (数字运算)
'1' * 2; // 2 (数字运算)
'1' / 2; // 0.5 (数字运算)
'1' % 2; // 1 (数字运算)
true + 1; // 2
false + 1; // 1
null + 1; // 1
undefined + 1; // NaN
'1' == 1; // true (类型转换后比较)
'1' === 1; // false (严格相等,不转换类型)
null == undefined; // true
null === undefined; // false
0 == false; // true
0 === false; // false
'' == false; // true
'' === false; // false
'hello' && 'world'; // "world" (返回最后一个真值)
'hello' || 'world'; // "hello" (返回第一个真值)
'' && 'world'; // "" (返回第一个假值)
'' || 'world'; // "world" (返回第一个真值)
0 && 1; // 0
0 || 1; // 1
==(相等运算符)
// == 的转换规则
null == undefined; // true
null == 0; // false
undefined == 0; // false
'0' == 0; // true
'' == 0; // true
false == 0; // true
[] == 0; // true
[] == ''; // true
[] == false; // true
[1] == 1; // true
[1,2] == '1,2'; // true
===(严格相等运算符)
'0' === 0; // false
'' === 0; // false
false === 0; // false
null === undefined; // false
从高到低:
// 优先级示例
let a = 1;
let b = 2;
let c = 3;
console.log(a + b * c); // 7 (先乘后加)
console.log((a + b) * c); // 9 (括号改变优先级)
console.log(a || b && c); // 1 (&& 优先级高于 ||)
console.log((a || b) && c); // 3
console.log(a > b ? a : b); // 2
let a = (1, 2, 3); // a = 3
let b = (x = 1, y = 2, x + y); // b = 3
function test() {
return (console.log('1'), console.log('2'), 'result');
}
test(); // 输出: 1, 2,返回 'result'
void 0; // undefined
void(1 + 2); // undefined
// 常用于立即执行函数
void function() {
console.log('IIFE');
}();
typeof 123; // "number"
typeof 'hello'; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof null; // "object" (历史遗留问题)
typeof {}; // "object"
typeof []; // "object"
typeof function(){};// "function"
[] instanceof Array; // true
{} instanceof Object; // true
[] instanceof Object; // true
function Person() {}
const p = new Person();
p instanceof Person; // true
[] + []; // "" (转换为字符串后拼接)
[] - []; // 0 (转换为数字后相减)
[1] + [2]; // "12"
[1,2] + [3,4]; // "1,23,4"
Number([]); // 0
Number([1]); // 1
Number([1,2]); // NaN
{} + {}; // "[object Object][object Object]"
{} + []; // 0 (特殊情况)
[] + {}; // "[object Object]"
Number({}); // NaN
String({}); // "[object Object]"
if ([]) {
console.log('true'); // 执行
}
if ({}) {
console.log('true'); // 执行
}
if (0) {
console.log('false'); // 不执行
}
if ('') {
console.log('false'); // 不执行
}
// 好的做法
if (value === 0) { }
if (value === null) { }
// 避免
if (value == 0) { }
// 好的做法
const num = Number(str);
const str = String(num);
const bool = Boolean(value);
// 避免隐式转换
const num = +str;
const str = num + '';
const bool = !!value; // 可以接受
// 检查类型
if (typeof value === 'number') { }
if (Array.isArray(value)) { }
if (value instanceof Date) { }