DEV Community

Cover image for Understanding JavaScript Data Types
Shaon Kabir
Shaon Kabir

Posted on

Understanding JavaScript Data Types

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

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.

    let handleName = "shaonkabir8";
    
  2. Number: Represents numeric values, including integers and floating-point numbers. Numbers are used for mathematical computations and numerical data representation.

    let age = 26;
    
  3. Boolean: Represents logical values indicating true or false. Booleans are primarily used for conditional statements and logical operations.

    let isMarried = false;
    
  4. Undefined: Represents a variable that has been declared but not assigned a value. It indicates the absence of a value.

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

    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.

    // 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.

    // 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.

    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.

    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.

    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
2.MDN Web Docs

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

You've missed two primitive data types - Symbol and BigInt

Collapse
 
shaonkabir8 profile image
Shaon Kabir

Thanks for correction.
Your comments is inspiration for me to write more dev article. Take love mate.

Collapse
 
sid555 profile image
Sidharth

Great share