MSDN Documentation

Mastering JavaScript: Core Concepts

JavaScript Data Types

Understanding JavaScript's data types is fundamental to writing effective and predictable code. JavaScript is a dynamically typed language, meaning you don't need to declare the type of a variable explicitly; the interpreter infers it at runtime. This section explores the primary data types available in JavaScript.

Primitive Data Types

Primitive data types are immutable, meaning their values cannot be changed directly. When you perform operations that seem to modify a primitive value, you are actually creating a new value.

1. String

Represents a sequence of characters. Strings are enclosed in single quotes ('), double quotes ("), or backticks (`).

Example:

let greeting = "Hello, World!";

let message = 'This is a message.';

let template = \`User: ${greeting}\`;

Output:

greeting will be "Hello, World!".

message will be "This is a message.".

template will be "User: Hello, World!".

2. Number

Represents numeric values. This includes integers and floating-point numbers. JavaScript also has special numeric values like Infinity, -Infinity, and NaN (Not-a-Number).

Example:

let age = 30;

let price = 19.99;

let result = 10 / 0; // Infinity

let notANumber = 0 / 0; // NaN

Output:

age will be 30.

price will be 19.99.

result will be Infinity.

notANumber will be NaN.

3. Boolean

Represents a logical value: true or false. Booleans are often the result of comparisons.

Example:

let isAdult = true;

let hasPermission = false;

let isGreater = 10 > 5; // true

Output:

isAdult will be true.

hasPermission will be false.

isGreater will be true.

4. Undefined

A variable that has been declared but not yet assigned a value has the type undefined. It can also be explicitly assigned.

Example:

let unassignedVariable;

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

let explicitlyUndefined = undefined;

Output:

unassignedVariable will be undefined.

explicitlyUndefined will be undefined.

5. Null

Represents the intentional absence of any object value. It is an assignment value.

Example:

let emptyValue = null;

console.log(typeof emptyValue); // "object" (a historical quirk!)

Output:

emptyValue will be null.

Important Note: While typeof null returns "object", null is a primitive value, not an object. This is a well-known peculiarity in JavaScript's type system.

6. Symbol

Introduced in ECMAScript 6 (ES6), Symbols are unique and immutable primitive values that can be used as identifiers for object properties. They are guaranteed to be unique.

Example:

const id1 = Symbol('description');

const id2 = Symbol('description');

console.log(id1 === id2); // false

Output:

id1 and id2 are unique Symbols, even with the same description.

7. BigInt

Introduced to handle integers larger than the maximum safe integer limit supported by the Number type. You can create a BigInt by appending n to an integer literal or by calling the BigInt() function.

Example:

const largeNumber = 9007199254740991n;

const anotherLarge = BigInt('12345678901234567890');

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

Output:

largeNumber and anotherLarge are bigint types.

Object Data Type

Unlike primitives, the Object type is not primitive. Objects are collections of properties, where each property is a key-value pair. These are mutable.

Example:

let person = { name: "Alice", age: 25 };

let today = new Date();

let numbers = [1, 2, 3]; // Arrays are a special type of object

Output:

person is an object literal.

today is a Date object.

numbers is an array, which is also an object.

Tip: Use the typeof operator to determine the data type of a variable. For example, typeof myVariable will return a string indicating the type (e.g., "string", "number", "boolean", "object", "undefined", "function", "symbol", "bigint"). Remember the exception for null.