In JavaScript, a primitive value is a data type that represents a single, immutable value. There are six primitive data types in JavaScript:
undefined
: Represents the absence of a defined value.null
: Represents the intentional absence of any object value.boolean
: Represents a logical value, eithertrue
orfalse
.number
: Represents numeric values, including integers and floating-point numbers.string
: Represents textual data enclosed in single quotes (''), double quotes ("") or backticks (``).symbol
: Represents a unique and immutable value that can be used as an identifier for object properties.
Primitive values are directly stored in memory and are not objects. They have a fixed size in memory and are compared by their actual value.
For example:
let myUndefined = undefined;
let myNull = null;
let myBoolean = true;
let myNumber = 10;
let myString = "Hello";
let mySymbol = Symbol("unique");
console.log(typeof myUndefined); // Output: "undefined"
console.log(typeof myNull); // Output: "object" (a quirk in JavaScript)
console.log(typeof myBoolean); // Output: "boolean"
console.log(typeof myNumber); // Output: "number"
console.log(typeof myString); // Output: "string"
console.log(typeof mySymbol); // Output: "symbol"
These primitive values form the building blocks of JavaScript and are used to represent basic data types and values in the language.