What are the different data types in JavaScript?
Asked on Jul 02, 2024
Answer
JavaScript has several data types that can be categorized into primitive and non-primitive types. Primitive types include String, Number, Boolean, Null, Undefined, Symbol, and BigInt, while non-primitive types include Objects.
// Primitive data types
const str = "Hello, World!"; // String
const num = 42; // Number
const bool = true; // Boolean
const nothing = null; // Null
const notDefined = undefined; // Undefined
const unique = Symbol("id"); // Symbol
const bigIntNum = 9007199254740991n; // BigInt
// Non-primitive data type
const obj = { name: "Alice", age: 30 }; // Object
Additional Comment:
✅ Answered with JavaScript best practices.- Primitive types are immutable, meaning their values cannot be changed.
- Objects are mutable and can hold collections of values and more complex entities.
- "Symbol" is used for creating unique identifiers.
- "BigInt" is used for numbers larger than the maximum safe integer in JavaScript.
- "Null" represents the intentional absence of any object value.
- "Undefined" indicates a variable that has been declared but not assigned a value.
Recommended Links:
← Back to All Questions