Question

11.

What are Primitive Data Types in JavaScript

Answer

In JavaScript, primitive data types represent the simplest forms of data. These types are not objects and have no methods. There are seven primitive data types in JavaScript:

  1. Undefined: Represents an uninitialized variable or a missing value. A variable that has been declared but not assigned a value is of type undefined.

    let a;
    console.log(a); // undefined
  2. Null: Represents the intentional absence of any object value. It is often used to signify an empty or unknown value.

    let a = null;
    console.log(a); // null
  3. Boolean: Represents a logical entity and can have two values: true and false.

    let a = true;
    let b = false;
    console.log(a, b); // true false
  4. Number: Represents both integer and floating-point numbers. JavaScript uses double-precision 64-bit binary format IEEE 754 values for numbers.

    let a = 25;
    let b = 80.5;
    console.log(a, b); // 25 80.5

    Additionally, there are special numeric values within this type that include Infinity, -Infinity, and NaN (Not-a-Number).

  5. BigInt: Introduced in ECMAScript 2020, this type represents integers with arbitrary precision. It can be used for safely storing and operating on large integers even beyond the safe integer limit for Numbers.

    let a = 9007199254740991n;
    let b = a + 1n;
    console.log(b); // 9007199254740992n
  6. String: Represents textual data. It is a set of "elements" of 16-bit unsigned integer values, where each element in the String occupies a position in the sequence. Strings in JavaScript are immutable.

    let a = "Hello, world!";
    console.log(a); // Hello, world!
  7. Symbol: Introduced in ECMAScript 2015, this type represents unique, immutable identifiers. Symbols are often used to add unique property keys to an object that won’t collide with keys any other code might add to the object, and which are hidden from any mechanisms other code will typically use to access the object.

    let a = Symbol("description");
    let b = Symbol("description");
    console.log(a === b); // false

Each of these types serves a specific purpose in JavaScript programming, and understanding their characteristics and behavior is fundamental to effective JavaScript development. Unlike objects, primitives are immutable: when you manipulate a primitive value, what you get back is a new primitive, not a modification of the original value.

Support us ❤️

Buy Us A Coffee