Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Difference between Array and Array of Objects in JavaScript
Next article icon

Difference Between Variables and Objects in JavaScript

Last Updated : 13 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Characteristics

Variable

Object

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.


Next Article
Difference between Array and Array of Objects in JavaScript
https://media.geeksforgeeks.org/auth/avatar.png
Anonymous
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-basics

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 var and let in JavaScript
    In the early days of JavaScript, there was only one way of declaring variables and that was using the var keyword. A variable declared with var is defined throughout the program. One of the issues with using the var keyword was redeclaring a variable inside a block will also redeclare the variable o
    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
    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 c
    4 min read
  • Difference between Node.js and JavaScript
    JavaScript and Node.js are both crucial in modern web development, but they serve different purposes and are used in different environments. JavaScript is a programming language primarily used for client-side web development, while Node is a runtime environment that allows JavaScript to be executed
    3 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
    3 min read
  • Difference between var, let and const keywords in JavaScript
    JavaScript provides three ways to declare variables: var, let, and const, but they differ in scope, hoisting behaviour, and re-assignment rules. Understanding these differences helps write more predictable and maintainable code. What is var, let and const in JavaScript?var: Declares variables with f
    6 min read
  • Difference Between Object.assign and Spread Operator in JavaScript
    The Object.assign() and the Spread Operator (...) are commonly used for copying properties from one object to another. While they can achieve similar outcomes, they have distinct characteristics and use cases. This article will explain the differences between the Object.assign() and the spread opera
    3 min read
  • Difference between Object.freeze() and const in JavaScript
    ES6 has brought several new features and methods into JavaScript since its release. Amongst these new features are Object.freeze() method and const. Sometimes people get confused between Object.freeze() method and const but the Object.freeze() and const are completely different. In this article we w
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences