在程序語言中,數據類型是基礎,一切程序都是建立在基礎數據之上。
如果說程序如同萬丈高樓平地起,那么數據類型就像沙、石、鋼筋、水泥等等最基礎的原料。一樣的高樓,不同的人,用相同的原料,造的方法也會有千般變化。
在 JS 中,數據類型可以分為 原始類型
和 對象類型
。
原始類型
直接存儲值,不可變(值的地址不可變),共 7 種:
1、number
數值類型,包括整數、浮點數、Infinity、NaN。
const num1 = 123;
const num2 = 123.456;
const num3 = Infinity;
const num4 = NaN;
const num5 = new Number(456);
console.log(typeof num5);
const num6 = Number(456);
console.log(typeof num6);
2、string
字符串類型。單雙引號聲明的字符串不允許換行,可使用反引號申明多行字符串和模版字符串。
const str1 = 'hello';
const str1_1 = '\'hello\'';
const str2 = " world";
const str3 = str1 + str2;
const str4 = `前端路引
${str1}${str2}`;
const str5 = new String('前端路引');
console.log(typeof str5);
const str6 = String('前端路引');
console.log(typeof str6);
3、boolean
布爾值(true/false)。
const bool1 = true;
const bool2 = false;
const bool3 = new Boolean(true);
console.log(typeof bool3);
const bool4 = Boolean(true);
console.log(typeof bool4);
4、null
表示空值。
const empty = null;
console.log(typeof empty);
5、undefined
未定義的值。
let u1;
const u2 = undefined;
6、symbol
唯一且不可變的值(符號)。就算使用 Symbol 聲明的內容一樣,但是兩個變量其實是不相等的!!
const sym1 = Symbol('前端路引');
const sym2 = Symbol('前端路引');
console.log(sym1 === sym2);
const sym3 = Symbol.for('前端路引');
const sym4 = Symbol.for('前端路引');
console.log(sym3 === sym4);
console.log(Symbol.keyFor(sym3));
const sym5 = Symbol();
7、bigint
大整數(以 n 結尾,如 123n),一般用于表示大于 2^53 - 1
的整數,ES2020+ 引入的新的數據類型,使用時需注意兼容性。
const big1 = 123n;
const big2 = BigInt(123);
console.log(big1 === big2);
console.log(typeof big1);
console.log(big1 === 123)
console.log(big1 === 123n);
對象類型
存儲引用(內存地址),可變,包含所有非原始類型的值:
1、普通對象
const obj1 = {};
const obj2 = { name: '前端路引', age: 1 };
const obj3 = new Object();
const obj4 = Object({name: '前端路引'});
2、數組
const arr1 = [];
const arr2 = [1, 2, 3];
const arr3 = new Array();
const arr4 = Array(10).fill('前端路引');
3、函數
function func1() {
console.log('Function 1');
}
const func2 = function() {
console.log('Function 2');
};
const func3 = () => {
console.log('Function 3');
};
除了基礎的三種基礎對象類型外,JS 還內置了很多其他對象,比如 Date、RegExp、Error、Map、Set、WeakMap、WeakSet、Promise、Proxy、ArrayBuffer 等。
類型轉換
JS 的類型轉換分為隱式轉換(明確表明由 A 轉為 B)和顯式轉換(自動發生的類型轉換)。
顯式轉換
通過對象方法強制轉換:
1、轉字符串
String(123);
[1,2].toString();
2、轉數字
Number("123");
Number("abc");
parseInt("14px");
3、轉布爾
Boolean("");
Boolean({});
隱式轉換
一半多發生于運算符,比如:
1、字符串拼接
console.log('1' + 1);
console.log(1 + '1');
2、數學運算
console.log('1' - 1);
console.log(1 - '1');
console.log('1' * 1);
console.log(1 * '1');
console.log('1' / 1);
console.log(1 / '1');
3、邏輯運算
if (0) {
console.log('0');
}
常見轉換規則
原始值 | 轉字符串 | 轉數字 | 轉布爾值 |
---|
true | "true" | 1 | true |
false | "false" | 0 | false |
0 | "0" | 0 | false |
"" | "" | 0 | false |
"123" | "123" | 123 | true |
null | "null" | 0 | false |
undefined | "undefined" | NaN | false |
NaN | "NaN" | NaN | false |
[] | "" | 0 | true |
[5] | "5" | 5 | true |
{} | "[object Object]" | NaN | true |
常見陷阱與最佳實踐
1、==
vs ===
==
會進行類型轉換: 0 == false
為 true
。===
嚴格比較類型和值,推薦使用。
2、NaN
的判斷
NaN === NaN
為 false
,使用 Number.isNaN(value)
或 Object.is(value, NaN)
。
3、對象轉換
- 對象轉原始值時,優先調用
valueOf()
,再 toString()
。 {} + []
可能被解析為代碼塊,導致結果意外。
4、parseInt基數
- 總是指定基數:
parseInt("08", 10)
避免八進制誤解。
寫在最后
由于 JavaScript 屬于弱類型語言,所以在編碼時候特別需要注意類型轉換問題。特常見問題:后端返回的數據類型是字符串 '1'
,在前端當做數字 1
使用,這時候分分鐘踩雷。
轉自https://www.cnblogs.com/linx/p/18890277
該文章在 2025/6/5 9:53:36 編輯過