JavaScript的資料型態可以分為:
原始資料型態(Primitive Data Types)和複合資料型態(Composite Data Types)
以下是每種資料型態的簡介:
數字(Number)
let age = 25;
let price = 19.99;
字符串(String)
let greeting = "Hello, world!";
let name = 'Alice';
let message = `Welcome, ${name}!`;
布爾值(Boolean)
true
和 false
。let isActive = true;
let isCompleted = false;
空值(null)
let emptyValue = null;
未定義(undefined)
let notAssigned;
console.log(notAssigned); // undefined
符號(Symbol)(ES6 引入)
const uniqueSymbol = Symbol('description');
對象(Object)
let person = {
name: 'Alice',
age: 30,
isStudent: false
};
console.log(person.name); // Alice
數組(Array)
let fruits = ['apple', 'banana', 'cherry'];
console.log(fruits[1]); // banana
函數(Function)
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // 5