JavaScript Tutorial

literals

literals are a concise syntax for creating values or data structures directly in code, without using constructors or variables. They represent fixed values that can be assigned to variables, used in expressions, or as part of objects/arrays. Here's a breakdown of common literals:

  1. String Literals Use quotes (', ", or `) to define text.

    Single/double quotes: 'Hello', "World". Template literals: Hello ${name} (supports interpolation).

  2. Numeric Literals Represent integers, decimals, or special forms:

    Integers: 42, 0b1010 (binary), 0xA (hex). Floats: 3.14, 2.5e3 (scientific notation). BigInt: 123n (for large integers).

  3. Boolean Literals true or false to represent logical values.

  4. Object Literals Create objects with {} syntax:

const str = 'integer';
const num = 42;
const obj = { key: 'value' };
const arr = [1, 2, 3];