- Home
- /
- Primitives and Objects
JavaScript Tutorial
IINTRODUCTION
JAVASCRIPT VARIABLES
JAVASCRIPT BASICS
JAVASCRIPT OBJECTS
DOM & BOM
OOPs
- Home
- /
- Primitives and Objects
Primitives and Objects
When working with JavaScript, you’ll frequently come across two broad types of data: primitives and objects. Understanding the difference between them is key to writing clean and reliable code.
What Are Primitives?
Primitives are the most basic data types in JavaScript. They represent single values and are stored directly in memory. The main primitive types include:
Numbers – like
10
or3.14
Strings – like
"hello"
or'world'
Booleans –
true
orfalse
null
– a value that explicitly means “no value”undefined
– used when a variable is declared but not assigned
One important thing to know: primitives are immutable. This means that their actual values can’t be changed. If you reassign a primitive, you’re not modifying it—you’re replacing it with a new one.
let x = 10;
x = 20; // This doesn't change 10; it assigns a new value to x
What Are Objects?
Objects are more flexible and powerful. They can store multiple values and even other data types—including primitives and other objects.
Objects are made up of key-value pairs. Here’s an example:
let user = {
name: "Arshyan",
age: 25
};
In this example, name
and age
are keys, and "Arshyan"
and 25
are their corresponding values.
Unlike primitives, objects are mutable. That means you can update their properties without creating a new object:
user.age = 26; // The object is updated
Objects in JavaScript also include:
Arrays:
let arr = [1, 2, 3];
Functions:
function greet() { console.log("Hi"); }
Dates:
let today = new Date();
All of these are technically considered objects because they can store and manage complex data.
Final Thoughts
Think of primitives as values that are simple, direct, and fixed once assigned—numbers, strings, booleans, etc. Meanwhile, objects are like containers that can hold multiple pieces of data and be changed freely.
Grasping this distinction will help you avoid bugs and better understand how data flows through your JavaScript programs.