A primitive value is a value that has no properties or methods. It is immutable, meaning it cannot be altered. JavaScript has 7 primitive data types.
The string type is used to represent textual data. A string in JavaScript must be surrounded by quotes (single, double, or backticks).
let name = "John";
let greeting = `Hello, ${name}!`; // Template literal
console.log(typeof greeting); // "string"
The number type represents both integer and floating-point numbers. Besides regular numbers, it also includes "special numeric values" like Infinity, -Infinity, and NaN (Not-a-Number).
let age = 30; let price = 19.99; let result = 1 / 0; // Infinityconsole.log(typeof age); // "number"
The BigInt type is used to represent whole numbers larger than 2^53 - 1, which is the largest number JavaScript can reliably represent with the number type. A BigInt is created by appending n to the end of an integer.
const bigNumber = 90071992547409915n;console.log(typeof bigNumber); // "bigint"
The boolean type has only two values: true and false. It is typically used for conditional logic.
let isDataSaved = true; let isEditorOpen = false;console.log(typeof isDataSaved); // "boolean"
A variable that has been declared but has not yet been assigned a value has the value undefined.
let user;console.log(user); // undefined console.log(typeof user); // "undefined"
The null value represents the intentional absence of any object value. It is a primitive value, but typeof null returns "object" due to a historical bug in JavaScript.
let selectedCar = null;console.log(selectedCar); // null console.log(typeof selectedCar); // "object" (the bug!)
The Symbol type is used to create unique identifiers for objects. Every Symbol() call is guaranteed to return a unique Symbol.
const id1 = Symbol("id");
const id2 = Symbol("id");
console.log(id1 === id2); // false
console.log(typeof id1); // "symbol"
Which of the following is NOT a primitive data type in JavaScript?