🟨 JavaScript Q101 / 104

What primitive types does JavaScript support?

AI-Powered Answer ✓ Answered

JavaScript, a high-level, interpreted programming language, categorizes data into two main groups: primitive types and object types. Primitive values are immutable, meaning their value cannot be changed after creation. Understanding these fundamental types is crucial for writing effective JavaScript code. There are seven primitive data types supported by JavaScript.

The Seven JavaScript Primitive Types

Below is a detailed explanation of each of the seven primitive data types available in JavaScript, along with their characteristics and common use cases.

1. String

Represents textual data. Strings are immutable sequences of Unicode characters. They can be created using single quotes (''), double quotes (" "), or backticks (``) for template literals, which allow for embedded expressions.

2. Number

Represents both integer and floating-point numbers. JavaScript uses double-precision 64-bit binary format IEEE 754 for numbers. This type includes special values like Infinity (positive infinity), -Infinity (negative infinity), and NaN (Not-a-Number), which represents an invalid or unrepresentable number.

3. BigInt

Introduced in ES2020, BigInt is a primitive type that can represent integers with arbitrary precision. This means BigInt numbers can store values larger than 2^53 - 1, which is the maximum safe integer for the Number type. BigInt numbers are created by appending n to the end of an integer literal (e.g., 10n).

4. Boolean

Represents a logical entity and can have only two values: true or false. These are fundamental for conditional logic, control flow statements, and expressing truthiness or falsiness in operations.

5. Undefined

A variable that has been declared but has not yet been assigned a value is undefined. It signifies the absence of a value, often indicating that a variable lacks a meaningful assignment or a function parameter was not provided.

6. Symbol

Introduced in ES6 (ECMAScript 2015), a Symbol is a unique and immutable data type. Symbols are often used to create unique object property keys that prevent name clashes in objects, especially when mixing code from different libraries or modules.

7. Null

Represents the intentional absence of any object value. null is a primitive value and is often explicitly assigned to a variable or property to indicate that it has no value or points to nothing. It's important to note that typeof null returns "object", which is a historical quirk in JavaScript.

These seven primitive types are the foundational building blocks for data manipulation in JavaScript. They are distinct from object types, which are mutable and more complex, allowing for properties and methods.