Difference Between Variables and Objects in JavaScript
Last Updated :
13 Dec, 2023
The variables and objects are fundamental concepts but they serve different purposes. The Variables are used to store data values while objects are used to group related data and functions into a single entity.
JavaScript Variable
A variable in JavaScript is a named container that stores a value. It can be created using the var, let, or const.
Syntax:
let variable_name = value;
const variable_name = value;
Example: In this example, age is a variable that holds a numeric value and name is a variable holding the string value.
JavaScript
let age = 35;
const name = 'Kumar';
console.log("Age: " + age);
console.log("Name: " + name);
Output:
Age: 35
Name: Kumar
JavaScript Object
An object in JavaScript is a complex data structure that groups related data together using the key-value pairs.
Syntax :
let object_name = {
key1 : value1,
key1 : value1,
...
};
Example: In this example, The person is an object with the properties like name, age, hobbies and address. It includes both the primitive data types and other objects.
JavaScript
let person = {
name: 'Kumar',
age: 25,
hobbies: ['reading', 'painting'],
address: {
street: '127 Main St',
city: 'Bangalore City'
}
};
console.log("Name: " + person.name);
console.log("Age: " + person.age);
console.log("Hobbies: " + person.hobbies.join(', '));
console.log("Street: " + person.address.street);
console.log("City: " + person.address.city);
Output:
Name: Kumar
Age: 25
Hobbies: reading, painting
Street: 127 Main St
City: Bangalore City
Difference between variables and object in JavaScript
|
Definition
| Containers for the storing single data values.
| The Complex data structures that store collections of the data and entities.
|
Usage
| The Holds single data values.
| The Stores complex entities and data structures.
|
Declaration
| Declared using the keywords like var, let, const.
| To Declared by defining key-value pairs within the curly braces.
|
Mutability
| Can be updated or reassigned with the different values.
| Properties can be added, updated or deleted.
|
Example
| let num = 10;
const name = 'John';
| let person = { name: 'Alice', age: 25 };
|
Access
| Access using the variable name.
| Access properties using the dot notation or bracket notation.
|
Data Type
| Can hold different data types.
| Can contain any data type including the other objects.
|
Purpose
| Used for the storing individual values.
| Used for the grouping related data or entities together.
|
Similar Reads
Difference Between Objects and Prototypes in JavaScript Objects in JavaScriptThe Objects in JavaScript are instances of the class or constructors and they can hold properties and methods. These properties and methods can be unique to the object or inherited from the prototypes. The Objects can be created using the constructor functions, object literals,
3 min read
Difference between Array and Array of Objects in JavaScript ArrayAn Array is a collection of data and a data structure that is stored in a sequence of memory locations. One can access the elements of an array by calling the index number such as 0, 1, 2, 3, ..., etc. The array can store data types like Integer, Float, String, and Boolean all the primitive dat
3 min read
Difference Between JavaScript Arrays and Objects Below are the main differences between a JavaScript Array and Object.FeatureJavaScript ArraysJavaScript ObjectsIndex TypeNumeric indexes (0, 1, 2, ...)Named keys (strings or symbols)OrderOrdered collectionUnordered collectionUse CaseStoring lists, sequences, ordered dataStoring data with key-value p
1 min read
Differences Between Undeclared and Undefined Variables in JavaScript In JavaScript, variables are declared using keywords like var, let, or const. The scope and behavior of variables depend on how they are declared:var: Has function scope or global scope. If not declared explicitly, a var variable becomes an undeclared global variable when assigned a value.let and co
4 min read
Difference Between for...in and Object.keys() in JavaScript The for...in and Object.keys() in JavaScript are used to iterate over the properties of an object. While they might seem similar at first glance they have distinct usage, behavior, and characteristics. This article will explore these differences in detail.These are the following topics that we are g
3 min read