JavaScript Data Types
In JavaScript, there are several data types that are used to represent different kinds of values. Here's an overview of the main data types in JavaScript:
- Primitive Data Types:
- Number: Represents numeric data, including integers and floating-point numbers.
- String: Represents textual data, enclosed within single or double quotes.
- Boolean: Represents a logical value, either true or false.
- Undefined: Represents a variable that has been declared but not assigned a value.
- Null: Represents the intentional absence of any object value.
- Symbol (introduced in ECMAScript 6): Represents a unique identifier.
- Composite Data Types:
- Object: Represents a collection of key-value pairs, where values can be primitives or other objects. Objects can be created using object literals {}, the new Object() syntax, or through constructors.
- Array: Represents a list-like collection of elements, where elements can be of any data type. Arrays can be created using array literals [] or the new Array() syntax.
- Function: Represents executable code and can be treated as objects. Functions can be defined using function declarations, function expressions, arrow functions, etc.
- Special Data Types:
- BigInt (introduced in ECMAScript 2020): Represents arbitrary-precision integers. It allows representing integers beyond the safe integer limit of Number type.
- Symbol (as a primitive type): Represents a unique identifier, primarily used as object property keys to avoid name clashes.
Example
// Numbers: let length = 16; let weight = 7.5; // Strings: let color = "Yellow"; let lastName = "Johnson"; // Booleans let x = true; let y = false; // Object: const person = {firstName:"John", lastName:"Doe"}; // Array object: const cars = ["Saab", "Volvo", "BMW"]; // Date object: const date = new Date("2022-03-25");You can click on above box to edit the code and run again.