JavaScript 有多種內建資料型,可分為兩大類:
?原始型別
?非原始(參考)類型。
Type | Examples |
---|---|
Primitive Types |
➀ Number ➁ String ➂ Boolean ➃ Undefined ➄ Null |
Non-Primitive Types |
➀ Object ➁ Array ➂ Function |
➂ 功能
❐ 現在編寫帶有詳細資訊的原始類型
原始資料型別是不可變的並依值儲存。
let age = 25; // Integer let pi = 3.14159; // Floating-point let negativeNumber = -42; // Negative number let exponential = 1.23e4; // 12300 in exponential notatio
✚ 數字
let singleQuote = 'Hello, world!'; let doubleQuote = "JavaScript is awesome!"; let templateLiteral = `This is a template literal`; let multiLine = `This is a multi-line string.`; console.log(`Name: ${singleQuote}, Length: ${singleQuote.length}`);
✚ 字串
let isJavaScriptFun = true; let isOver18 = false; console.log(typeof isJavaScriptFun); // "boolean" console.log(5 > 2); // true console.log(10 === '10'); // false
➭ 代表邏輯值:true 或 false。
let x; console.log(x); // undefined console.log(typeof x); // "undefined"
➭ 宣告了變數但未賦值。
let y = null; console.log(y); // null console.log(typeof y); // "object" (this is a quirk in JavaScript)
❐ 現在編寫帶有詳細資訊的非原始類型
非原始資料型別是可變的並透過引用儲存..
let person = { name: 'John', age: 30, isStudent: false, hobbies: ['reading', 'gaming'], address: { city: 'New York', zip: '10001', }, }; console.log(person.name); // "John" console.log(person.address.city); // "New York" console.log(typeof person); // "object"
✚ 物件
let fruits = ['Apple', 'Banana', 'Cherry']; let mixedArray = [1, 'Hello', true, null, undefined]; console.log(fruits[0]); // "Apple" console.log(mixedArray.length); // 5 console.log(typeof fruits); // "object" (arrays are objects in JS)
✚ 陣列
function greet(name) { return `Hello, ${name}!`; } let sum = function(a, b) { return a + b; }; let multiply = (x, y) => x * y; console.log(greet('Alice')); // "Hello, Alice!" console.log(sum(3, 4)); // 7 console.log(multiply(5, 6)); // 30 console.log(typeof greet); // "function"
以上是了解 JavaScript 資料類型:帶有範例的基本類型和引用類型綜合指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!