JavaScript Data Types

JavaScript encompasses a rich assortment of data types that empower developers to manipulate and organize various kinds of information. These data types can be categorized into primitive types and reference types.

Primitive Data Types

These data types are fundamental and immutable, representing basic values directly.

Number

This data type covers both integers and floating-point numbers. Examples:

let integer = 42; let float = 3.14;

String

Strings represent sequences of characters, enclosed in single or double quotes. Examples:

let message = "Hello, world!"; let name = 'Alice';

Boolean

Booleans have two possible values: true or false. They're used for logical operations and conditions. Examples:

let isTrue = true; let isFalse = false;

Undefined

When a variable is declared but not assigned a value, it holds the undefined type. Example:

let uninitializedVar;

Null

The null type signifies an intentional absence of value. Example:

let noValue = null;

Symbol (ES6)

Symbols are unique and often used as object property identifiers to prevent naming conflicts. Example:

const uniqueKey = Symbol('unique');

Reference Data Types

These data types are more complex and can hold multiple values and methods. They are mutable, meaning their content can be modified.

Object

Objects are collections of key-value pairs, where keys are strings or symbols and values can be of any data type. Examples:

let person = { name: "John", age: 30, isStudent: false };

Array

Arrays are ordered lists of values, indexed starting from zero. They can hold different data types. Examples:

let colors = ["red", "green", "blue"]; let mixedArray = [42, "apple", true];

Function

Functions are reusable blocks of code that can be executed when called. Example:

function greet(name) { return `Hello, ${name}!`; }

Date

The Date type is used to work with dates and times. Example:

let currentDate = new Date();

RegExp

Regular expressions are used for pattern matching within strings. Example:

let pattern = /[a-z]+/;

Conclusion

JavaScript's diverse array of data types provides the foundation for creating intricate and efficient programs, enabling developers to manage different kinds of data and perform various operations.