In JavaScript, data types define the type of values that can be stored and manipulated by a program. Here are the main data types in JavaScript, along with details and examples:
Primitive Data Types:
Number: Represents numeric values, including integers and floating-point numbers.
let num = 10; let pi = 3.14;
String: Represents textual data enclosed within single (' ') or double (" ") quotes.
let message = 'Hello, world!'; let name = "John Doe";
Boolean: Represents a logical value indicating true or false.
let isTrue = true; let isFalse = false;
Undefined: Represents a variable that has been declared but not assigned a value.
let undefinedVar;
Null: Represents an intentional absence of any value.
let nullVar = null;
Symbol (ES6+): Represents unique identifiers and is often used as property keys in objects.
const sym = Symbol('description');
Composite Data Types:
Object: Represents a collection of key-value pairs, where keys are strings (or symbols) and values can be of any data type.
let person = { name: 'John', age: 30 };
Array: Represents an ordered list of values, accessible by numerical indices starting from 0.
let numbers = [1, 2, 3, 4, 5];
Special Data Types:
Function: Represents a reusable block of code that can be executed when called.
function greet(name) { console.log('Hello, ' + name + '!'); }
Date: Represents a specific date and time.
let today = new Date();
These are the main data types in JavaScript, each serving different purposes and allowing developers to work with various kinds of data effectively.
0 Comments