☜
☞
data type
In programming, a data type is a classification that specifies which type of value a variable or expression can hold. JavaScript has several built-in data types, including:
- String: a sequence of characters, enclosed in single or double quotes. For example: "hello" or 'world'.
// String
let name = "John";
console.log(name); // Output: "John"
- Number: numeric values, including integers and floating-point values. For example: 42 or 3.14.
// Number
let age = 30;
console.log(age); // Output: 30
- Boolean: a value that can be either true or false.
// Boolean
let isStudent = false;
console.log(isStudent); // Output: false
- Undefined: a value that indicates a variable has been declared but has not been assigned a value.
// Undefined
let address;
console.log(address); // Output: undefined
- Symbol: a unique and immutable data type that can be used as an identifier for object properties.
// Symbol
let sym = Symbol("uniqueId");
console.log(sym); // Output: Symbol(uniqueId)
- Object: a collection of properties and methods.
// Object
let person = {
name: "John",
age: 30
};
console.log(person); // Output: { name: "John", age: 30 }
- Array: a special type of object that holds a list of values.
// Array
let colors = ["red", "green", "blue"];
console.log(colors); // Output: ["red", "green", "blue"]
- Function: a block of code that can be executed when called.
// Function
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // Output: 5
JavaScript also supports a special data type called null, which represents the absence of a value or object.
It's also important to note that JavaScript is a loosely typed language, which means that variables do not have to be declared with a specific data type, and their data type can change at runtime.
☜
☞