DEV Community

Boomika N
Boomika N

Posted on

Data types in Java Script

What are Data Types?
Data types in JavaScript define the kind of value a variable can hold. They tell the JavaScript engine how to store and handle data.

Types of Data Types in JavaScript

JavaScript has 2 main categories:

  1. Primitive Data Types (Basic Types)

These store single values and are immutable.

✔ List of Primitive Types:

  1. Number
    let x = 10;
    let y = 3.14;

  2. String
    let name = "John";
    let city = 'Chennai';
    Template literals:
    let msg = Hello ${name};
    Strings are immutable

  3. Boolean
    Logical values:
    let isActive = true;
    let isDone = false;
    Used in:
    Conditions
    Comparisons

  4. Undefined
    A variable declared but not assigned:
    let x;
    console.log(x); // undefined

  5. Null
    Represents intentional absence of value:
    let data = null;
    Difference:
    undefined → no value assigned
    null → empty on purpose

  6. BigInt
    For very large numbers:
    let big = 123456789012345678901234567890n;
    Used when numbers exceed safe limit (Number.MAX_SAFE_INTEGER)

  7. Symbol
    Creates unique identifiers:
    let id1 = Symbol("id");
    let id2 = Symbol("id");
    console.log(id1 === id2); // false
    Always unique

What is Mutable and Immutable?

  • Mutable
    A mutable object can be changed after it is created.
    You can modify:
    Its values
    Its properties

  • Immutable
    An immutable value cannot be changed after creation.
    If you try to change it, you actually create a new value, not modify the original.

*Mutable in JavaScript (Detailed)
In JavaScript, non-primitive types are mutable:
Objects
Arrays
Functions
Example: Object (Mutable)
`let person = {
name: "John"
};

person.name = "Alex"; // modifying existing object

console.log(person.name); // Alex

✔ The original object is changed`
Mutable = same memory location is modified.

*Immutable in JavaScript (Detailed)
Primitive types are immutable:
Number
String
Boolean
Null
Undefined
BigInt
Symbol
Example: String (Immutable)
`let str = "hello";

str[0] = "H"; // trying to change

console.log(str); // "hello"

No change happens`

Top comments (0)