What is Primitive and Non-Primitive data type in javascript?

Muhaymin Bin Mehmood

Muhaymin Bin Mehmood

· 4 min read
Understanding Time Complexity of Array Operations in JavaScript banner image
Understanding Time Complexity of Array Operations in JavaScript banner image

Data types are fundamental building blocks for storing information in JavaScript. They define the kind of values variables can hold and determine how operations are performed on them. JavaScript includes two primary classifications of data types:


1. Primitive Data Types:

  • Fundamental, immutable values: They represent essential data units and cannot be changed directly.
  • Stored directly in memory: Have a fixed size determined by their type.
  • Six types:
    • number: Represents numeric values (integers, decimals, etc.).
    • string: Represents sequences of characters (text).
    • boolean: Represents true or false values.
    • undefined: Represents the absence of a value.
    • null: Indicates a purposeful absence of a value.
    • symbol: Represents unique, immutable identity values (introduced in ES6).

2. Non-Primitive Data Types:

  • Complex, mutable data structures: Can hold collections of values or references to other data.
  • Stored by reference in memory: Allocate a location in memory to store the reference, not the actual data.
  • One main type:
    • object: Everything else in JavaScript except primitives falls under this category, including:
      • arrays: Ordered collections of values.
      • functions: Reusable blocks of code that perform actions.
      • dates: Represents dates and times.
      • maps: Collections of key-value pairs.
      • sets: Collections of unique values.

Real-World Life Examples:

  • Primitive:
    • Numbers: Calculating product prices, distances, scientific data.
    • Strings: Representing names, text content, website URLs.
    • Booleans: Checking user input, controlling program flow (e.g., is_logged_in = true).
  • Non-Primitive:
    • Arrays: Shopping cart items, user profiles, storing multiple related data points.
    • Objects: Representing complex entities like users (with properties like name, email, address), products (with details like price, stock).
    • Functions: Implementing game logic, validating user input, performing repetitive tasks.

Explanation of Each Primitive Data Type:

1. Number:

  • Used for numerical calculations, including integers, decimals, and special values like Infinity and NaN.
  • Examples:

2. String:

  • Used for text, including single characters, words, and sentences.
  • Enclosed in single or double quotes.
  • Can be manipulated with string methods (e.g., toUpperCase(), substring(), concat()).
  • Examples:

3. Boolean:

  • Represents logical values: true or false.
  • Used for conditional statements and making decisions in code.
  • Can be compared using comparison operators (==, !=, ===, !==).
  • Examples:

4. Undefined:

  • Indicates the absence of a value, either because it hasn't been assigned or a variable has been declared but not initialized.
  • Using an undeclared variable directly results in an error.
  • Example:

5. Null:

  • Represents the intentional absence of a value, indicating "no value" exists.
  • Different from undefined: null is deliberately assigned, while undefined is the default state of uninitialized variables.
  • Examples:

6. Symbol:

  • Introduced in ES6, represents unique, immutable values that cannot be compared with other symbols or primitive values using strict equality (===).
  • Useful for creating objects with private properties or ensuring unique object keys.
  • Examples:

Conclusion:

Understanding primitive and non-primitive data types is crucial for effective JavaScript programming. Choosing the right type for your data ensures efficient memory usage, proper operations, and clear code behavior. By mastering these fundamental concepts, you can build well-structured and reliable JavaScript applications.

Muhaymin Bin Mehmood

About Muhaymin Bin Mehmood

I am a Front-End Developer specializing in Frontend at Dvago.pk.

Copyright © 2024 Mbloging. All rights reserved.