Javascript 數(shù)據(jù)類型介紹
Javascript 數(shù)據(jù)類型介紹
介紹
在Javascript中,有七種數(shù)據(jù)類型。分為兩類:基本數(shù)據(jù)類型和引用數(shù)據(jù)類型。基本數(shù)據(jù)類型包括:Undefined, Null, Boolean, Number, String和Symbol(ES6新增)。引用數(shù)據(jù)類型包括:Object。在本文中,我們將詳細(xì)介紹這些數(shù)據(jù)類型,并舉出30個例子來解釋它們。
基本數(shù)據(jù)類型
Undefined
Undefined表示聲明了一個變量但未對其進行初始化。例如:
let undefinedVariable;
console.log(undefinedVariable); // undefined
Null
Null表示一個空值。例如:
let nullVariable = null;
console.log(nullVariable); // null
Boolean
Boolean表示true或false的值。例如:
let booleanVariable = true;
console.log(booleanVariable); // true
Number
Number表示數(shù)字。例如:
let numberVariable = 123;
console.log(numberVariable); // 123
String
String表示文本字符串。例如:
let stringVariable = "Hello World!";
console.log(stringVariable); // Hello World!
Symbol
Symbol是ES6新增的數(shù)據(jù)類型,表示唯一的標(biāo)識符。例如:
let symbolVariable = Symbol("description");
console.log(symbolVariable); // Symbol(description)
引用數(shù)據(jù)類型
Object
Object表示一個對象。例如:
let objectVariable = { name: "John", age: 30 };
console.log(objectVariable); // { name: "John", age: 30 }
例子
下面是一些例子,演示了Javascript中各種數(shù)據(jù)類型的用法:
1. Undefined
Undefined表示聲明了一個變量但未對其進行初始化。
let undefinedVariable;
console.log(undefinedVariable); // undefined
2. Null
Null表示一個空值。
let nullVariable = null;
console.log(nullVariable); // null
3. Boolean
Boolean表示true或false的值。
let booleanVariable = true;
console.log(booleanVariable); // true
let booleanVariable = false;
console.log(booleanVariable); // false
4. Number
Number表示數(shù)字。
let numberVariable = 123;
console.log(numberVariable); // 123
let numberVariable = -123;
console.log(numberVariable); // -123
let numberVariable = 1.23;
console.log(numberVariable); // 1.23
5. String
String表示文本字符串。
let stringVariable = "Hello World!";
console.log(stringVariable); // Hello World!
let stringVariable = 'Hello World!';
console.log(stringVariable); // Hello World!
6. Symbol
Symbol是ES6新增的數(shù)據(jù)類型,表示唯一的標(biāo)識符。
let symbolVariable1 = Symbol("description");
let symbolVariable2 = Symbol("description");
console.log(symbolVariable1 === symbolVariable2); // false
7. Object
Object表示一個對象。
let objectVariable = { name: "John", age: 30 };
console.log(objectVariable); // { name: "John", age: 30 }
let objectVariable = { "name": "John", "age": 30 };
console.log(objectVariable); // { name: "John", age: 30 }
let objectVariable = { name: "John", age: 30 };
console.log(objectVariable.name); // John
console.log(objectVariable.age); // 30
let objectVariable = { name: "John", age: 30 };
objectVariable.name = "Mary";
console.log(objectVariable); // { name: "Mary", age: 30 }
let objectVariable = { name: "John", age: 30 };
objectVariable.email = "john@example.com";
console.log(objectVariable); // { name: "John", age: 30, email: "john@example.com"}
let objectVariable1 = { name: "John", age: 30 };
let objectVariable2 = { name: "Mary", age: 25 };
let arrayVariable = [objectVariable1, objectVariable2];
console.log(arrayVariable); // [{ name: "John", age: 30 }, { name: "Mary", age: 25 }]
let objectVariable = { name: "John", age: 30 };
console.log(Object.keys(objectVariable)); // ["name", "age"]
let objectVariable = { name: "John", age: 30 };
console.log(Object.values(objectVariable)); // ["John", 30]
let objectVariable = { name: "John", age: 30 };
console.log(JSON.stringify(objectVariable)); // {"name":"John","age":30}
let objectVariable = { name: "John", age: 30 };
console.log(Object.getOwnPropertyNames(objectVariable)); // ["name", "age"]
結(jié)論
在Javascript中,有七種數(shù)據(jù)類型?;緮?shù)據(jù)類型包括:Undefined, Null, Boolean, Number, String和Symbol(ES6新增)。引用數(shù)據(jù)類型包括:Object。使用這些數(shù)據(jù)類型,您可以創(chuàng)建各種不同類型的變量和對象。如果您想深入了解Javascript數(shù)據(jù)類型,可以參考MDN文檔。希望這篇文章對您有所幫助!
補充
在Javascript中,還有兩個特殊類型:NaN和Infinity。
NaN
NaN表示一個非數(shù)字值。例如:
let nanVariable = "hello" / 5;
console.log(nanVariable); // NaN
Infinity
Infinity表示一個無窮大的值。例如:
let infinityVariable = 1 / 0;
console.log(infinityVariable); // Infinity
let negativeInfinityVariable = -1 / 0;
console.log(negativeInfinityVariable); // -Infinity
補充內(nèi)容結(jié)束。
數(shù)據(jù)類型的相等比較
在Javascript中,數(shù)據(jù)類型的相等比較是非常重要的。以下是一些例子:
console.log(1 == "1"); // true
console.log(1 === "1"); // false
console.log(null == undefined); // true
console.log(null === undefined); // false
console.log(0 == false); // true
console.log(0 === false); // false
在進行相等比較時,使用“==”會進行類型轉(zhuǎn)換,而“===”不會進行類型轉(zhuǎn)換。因此,建議在進行相等比較時使用“===”。
數(shù)據(jù)類型的轉(zhuǎn)換
在Javascript中,數(shù)據(jù)類型的轉(zhuǎn)換是非常常見的。以下是一些例子:
字符串轉(zhuǎn)數(shù)字
let stringVariable = "123";
console.log(typeof stringVariable); // string
let numberVariable = Number(stringVariable);
console.log(typeof numberVariable); // number
數(shù)字轉(zhuǎn)字符串
let numberVariable = 123;
console.log(typeof numberVariable); // number
let stringVariable = numberVariable.toString();
console.log(typeof stringVariable); // string
布爾轉(zhuǎn)字符串
let booleanVariable = true;
console.log(typeof booleanVariable); // boolean
let stringVariable = booleanVariable.toString();
console.log(typeof stringVariable); // string
字符串轉(zhuǎn)布爾
let stringVariable = "true";
console.log(typeof stringVariable); // string
let booleanVariable = Boolean(stringVariable);
console.log(typeof booleanVariable); // boolean
數(shù)字轉(zhuǎn)布爾
let numberVariable = 123;
console.log(typeof numberVariable); // number
let booleanVariable = Boolean(numberVariable);
console.log(typeof booleanVariable); // boolean
結(jié)論
在Javascript中,數(shù)據(jù)類型的轉(zhuǎn)換是非常常見的??梢允褂脙?nèi)置函數(shù)進行轉(zhuǎn)換,例如:Number()、toString()、Boolean()等。在進行相等比較時,建議使用“===”來避免類型轉(zhuǎn)換。
希望這篇文章對您有所幫助!