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 Static and Const in JavaScript
Next article icon

Difference between Object.freeze() and const in JavaScript

Last Updated : 06 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 will explain the differences between these two.

JavaScript const: The const keyword creates a read-only reference to a value. Variables created by the const keyword are immutable. In other words, you can’t reassign them to different values. Trying to reassign a constant variable will result in a TypeError.

Syntax:

const const_name;  const x;

Example 1: 

JavaScript
let myName = "Geeksforgeeks" console.log(myName)   // Uncaught TypeError myName = "gfg"                     

Output:

Geeksforgeeks

The const keyword ensures that the variable created is read-only. But It doesn’t mean that the actual value to which the const variable reference is immutable. Even though the person variable is constant. However, you can change the value of its property. But you cannot reassign a different value to the person constant.

Example 2: 

JavaScript
const person = {     name: "Geeksforgeeks" };          // No TypeError person.name = "gfg"; console.log(person.name);                  

Output:

gfg

Object.freeze() method: If you want the value of the person object to be immutable, you have to freeze it by using the Object.freeze() method. 

Syntax:

Object.freeze(obj)

Example 1: 

JavaScript
const person = Object.freeze({name: "Geeksforgeeks"});     // TypeError in strict mode //in non-strict mode it prints "Geeksforgeeks" person.name = "gfg";  console.log(person.name) 

Output:

Geeksforgeeks

The Object.freeze() method is shallow, meaning that it can freeze the properties of the object, not the objects referenced by the properties. 

Example 2: 

JavaScript
const person = Object.freeze({     name: 'Geeksforgeeks',     address: {         city:"Noida"     } }); person.address.country = "India" console.log(person.address.country) 

But the person.address object is not immutable, you can add a new property to the person.address object as follows: 

// No TypeError  person.address.country = "India";

Output:

India

Conclusion: 

  • const prevents reassignment.
  • Object.freeze() prevents mutability.
 

Object.freeze()

const

1.Object freeze() method helps in preventing existing properties from being changedconst is a keyword that was introduced in ES6 (2015).
2.

Its syntax is -:

Object.freeze(object) 

It is helpful if we want some variable not to be declared twice.
3.It takes one parameter as an Object.

For example -:

const a = 10;

4.Its return value is an object.If we define a variable with const then it cannot be reassigned.
5.It also helps in preventing the new properties to be added to the specific object.If we define a variable with const then its scope is blocked.

Next Article
Difference Between Static and Const in JavaScript

K

kiranpuli
Improve
Article Tags :
  • Difference Between
  • JavaScript
  • Web Technologies
  • Web Technologies - Difference Between

Similar Reads

  • Difference Between Static and Const in JavaScript
    Static variable: A static variable in JavaScript is basically a property of the class which is not used on the object of the class but is used in the class itself. This static variable is stored into the data segment of the memory and its value is shared among all the objects/instances created in th
    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 Scope and Closures in JavaScript
    The scope and closures are fundamental concepts that play a crucial role in how variables are accessed and managed within the functions and blocks of code. In this article, we will learn about the difference between scope and closures in JavaScript. What is Scope?The Scope in JavaScript refers to th
    2 min read
  • What is the difference between freeze and seal in JavaScript?
    In JavaScript, Object.freeze makes an object immutable, preventing any changes to existing properties and values. Object.seal allows changes to existing properties but prevents adding or removing properties. Both methods enforce strict immutability, but freeze is stricter than seal. Table of Content
    2 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 Variables and Objects in JavaScript
    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 VariableA variable in JavaScript is a named container that stores a value. It
    2 min read
  • 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 label and break in JavaScript
    The labels and the break statement are used within loops and control flow structures to enhance flexibility and manageability. Each serves distinct purposes and can be applied in the various scenarios based on the needs of the code. These are the following topics that we are going to discuss: Table
    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
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