Understanding JavaScript Data Types
Tags: javascript, coding, programming, tutorial
Published: 2024-04-21 09:43:33

## Exploring Data Types in JavaScript

JavaScript, as a dynamically-typed language, offers a variety of data types that serve as the building blocks for storing and manipulating data. Understanding these data types is fundamental for effective programming and data handling. In this article, we'll delve into the diverse range of data types supported by JavaScript, including both primitive and non-primitive types.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdk3au77bd0l4emsd0t8.png)
### Primitive Data Types

Primitive data types are the fundamental data types built into the JavaScript language. They are immutable, meaning their values cannot be changed once they are created. Let's explore each primitive data type:

1. **String:** Represents sequences of characters enclosed within single ('') or double ("") quotes. Strings are commonly used for representing textual data.

    ```javascript
    let handleName = "shaonkabir8";
    ```

2. **Number:** Represents numeric values, including integers and floating-point numbers. Numbers are used for mathematical computations and numerical data representation.

    ```javascript
    let age = 26;
    ```

3. **Boolean:** Represents logical values indicating true or false. Booleans are primarily used for conditional statements and logical operations.

    ```javascript
    let isMarried = false;
    ```

4. **Undefined:** Represents a variable that has been declared but not assigned a value. It indicates the absence of a value.

    ```javascript
    let city;
    ```

5. **Null:** Represents the intentional absence of any value. It is often used to signify the absence of an object value.

    ```javascript
    let phoneNumber = null;
    ```

6. **Symbol:** Represents a unique and immutable data type introduced in ECMAScript 6 (ES6). Symbols are often used as property keys in objects to avoid naming conflicts. 

    ```javascript
    // Creating symbols with and without descriptions
    let symbol1 = Symbol();
    let symbol2 = Symbol("key");
    ```

7. **BigInt:** Represents numeric values with arbitrary precision beyond the range of the Number type. BigInts are created by appending `n` to the end of an integer literal or by calling the `BigInt()` constructor.  

    ```javascript
    // Creating BigInt values
    let bigInt1 = 1234567890123456789012345678901234567890n;
    let bigInt2 = BigInt("9007199254740991");
    ```


### Non-Primitive Data Types

Non-primitive data types, also known as reference data types, are more complex data structures compared to primitive types. They are mutable, meaning their values can be modified after creation. Let's explore some common non-primitive data types:

1. **Array:** Represents ordered collections of elements, which can be of any data type. Arrays are mutable and can dynamically grow or shrink in size.

    ```javascript
    let hobbies = ["coding", "reading", "gaming"];
    ```

2. **Object:** Represents collections of key-value pairs, where each key is a unique identifier for its corresponding value. Objects are mutable and can contain various data types, including other objects and arrays.

    ```javascript
    let person = {
      firstName: "Shaon",
      lastName: "Kabir",
      age: 26,
      isMarried: false,
    };
    ```

3. **Function:** Represents reusable blocks of code designed to perform specific tasks. Functions are first-class citizens in JavaScript and can be passed around as arguments or returned from other functions.

    ```javascript
    function greet(name) {
      console.log("Hello, " + name + "!");
    }
    ```

Understanding the distinction between primitive and non-primitive data types is crucial for effective data manipulation and programming in JavaScript. Leveraging JSDoc annotations can provide clear documentation of the data types used in your code, enhancing readability and maintainability.

---

Read more about the core concepts of Javascript (Data Types) throughout several different resources. Few important resources is shared to learn more efficiently.

1.[Data Type - javascript.info](https://javascript.info/types)
2.[MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures)