DEV Community

Cover image for ๐Ÿ‘‡Primitive and Non-Primitive Data Types
Himanay Khajuria
Himanay Khajuria

Posted on

๐Ÿ‘‡Primitive and Non-Primitive Data Types

โœ๐Ÿป Primitive and Non-Primitive Data Types in JavaScript

Hello everyone ๐Ÿ‘‹

Iโ€™m Himanay Khajuria, a Frontend Developer at SITA.dev, and today Iโ€™m going to share something really helpful for anyone who is new to JavaScript โžต understanding Primitive and Non-Primitive Data Types.

Whether you're just starting out or trying to revise your basics, this blog will help you understand these two important concepts in a very simple way with real-life examples. Let's get started! ๐Ÿ


โœ… What are Data Types?

Before we dive into primitive and non-primitive, let's understand what a data type is.

๐Ÿ‘‰ In simple words, data types tell JavaScript what kind of value a variable is holding.

Just like we know whether something is a number (like 5) or a word (like โ€œhelloโ€), JavaScript also needs to know what type of data youโ€™re using so it can treat it correctly.


๐ŸŸข Primitive Data Types

Primitive means "basic" or "simple". These are the simple and fixed data types in JavaScript.

๐Ÿ“‹ List of Primitive Data Types:

Data Type Example Description
String "Hello" Text or words
Number 42 Numbers (whole or decimal)
Boolean true / false Yes or No, True or False
Undefined undefined Value is not assigned yet
Null null Intentionally empty
Symbol Symbol("id") Unique identifier (advanced usage)
BigInt 12345678901234567890n Very large numbers

๐Ÿ” Real-Life Examples

๐Ÿ“Œ Letโ€™s understand with small relatable examples:

let name = "Himanay";         // String - like your name
let age = 25;                 // Number - your age
let isStudent = true;         // Boolean - Yes or No question
let job;                      // Undefined - not assigned yet
let emptyValue = null;        // Null - we know it's empty
let uniqueKey = Symbol("id"); // Symbol - unique key
let bigNumber = 12345678901234567890n; // BigInt - huge number
Enter fullscreen mode Exit fullscreen mode

These values are stored directly in memory, and they are immutable, meaning they cannot be changed.


๐Ÿ”ต Non-Primitive Data Types

Now letโ€™s look at Non-Primitive data types. These are more complex and can store multiple values.

๐Ÿ“‹ List of Non-Primitive Data Types:

Data Type Example Description
Object { name: "Himanay", age: 25 } Key-value pairs
Array [1, 2, 3] List of values
Function function() { console.log("Hi")} Block of reusable code

๐Ÿ” Real-Life Examples

๐Ÿ“Œ Think of these like boxes with many compartments:

let person = {
  name: "Himanay",
  age: 25
}; // Object - like your ID card with many details

let fruits = ["Apple", "Banana", "Mango"]; // Array - list of fruits

function greet() {
  console.log("Hello there!");
} // Function - like a machine that runs when called
Enter fullscreen mode Exit fullscreen mode

These are stored by reference in memory, and they are mutable, so you can change the values.


๐Ÿ”„ Key Differences at a Glance

Feature Primitive Non-Primitive
Stored by Value Reference
Can be changed? โŒ No (Immutable) โœ… Yes (Mutable)
Type of data Simple Complex
Examples String, Number, Boolean Object, Array, Function

๐Ÿงช Quick Checklist for Revision

  • [x] Data types tell JavaScript what kind of data we are using
  • [x] Primitive types are basic and fixed
  • [x] Non-Primitive types are complex and can hold multiple values
  • [x] Primitive is stored by value, Non-Primitive by reference
  • [x] We can change Non-Primitive values, but not Primitive ones

๐Ÿง  Pro Tip

๐Ÿ‘‰ Use typeof in JavaScript to check the type of value:

console.log(typeof "Himanay"); // Output: string
console.log(typeof 10);        // Output: number
console.log(typeof {});        // Output: object
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ด Final Thoughts

Understanding data types is like learning the alphabet before writing sentences. Once you know what kind of data you are working with, everything becomes easier โ•ฐโ”ˆโžค from storing values to debugging issues.

๐Ÿค— I hope this blog helped you understand Primitive and Non-Primitive Data Types in a beginner-friendly way. Keep practicing and you'll get better every day!

Feel free to connect with me or share your feedback! ๐Ÿ™Œ

Happy coding! ๐Ÿ‘จโ€๐Ÿ’ปโœจ

Top comments (0)