JS Primitive Data Types

JavaScript Primitive Data Types

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.


1. String

The string type is used to represent textual data. A string in JavaScript must be surrounded by quotes (single, double, or backticks).

String Example

let name = "John";
let greeting = `Hello, ${name}!`; // Template literal

console.log(typeof greeting); // "string"


2. Number

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).

Number Example

let age = 30;
let price = 19.99;
let result = 1 / 0; // Infinity

console.log(typeof age); // "number"


3. BigInt

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.

BigInt Example

const bigNumber = 90071992547409915n;

console.log(typeof bigNumber); // "bigint"


4. Boolean

The boolean type has only two values: true and false. It is typically used for conditional logic.

Boolean Example

let isDataSaved = true;
let isEditorOpen = false;

console.log(typeof isDataSaved); // "boolean"


5. Undefined

A variable that has been declared but has not yet been assigned a value has the value undefined.

Undefined Example

let user;

console.log(user); // undefined console.log(typeof user); // "undefined"


6. Null

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.

Null Example

let selectedCar = null;

console.log(selectedCar); // null console.log(typeof selectedCar); // "object" (the bug!)


7. Symbol

The Symbol type is used to create unique identifiers for objects. Every Symbol() call is guaranteed to return a unique Symbol.

Symbol Example

const id1 = Symbol("id");
const id2 = Symbol("id");

console.log(id1 === id2); // false console.log(typeof id1); // "symbol"


Exercise

?

Which of the following is NOT a primitive data type in JavaScript?