
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the main difference between Object.freeze() and const in JavaScript?
The difference between Object.freeze() and const is that the former prevents mutability whereas the latter doesn't prevent mutability. For better understanding lets' discuss them individually.
Const
Const behavior is same as that of let. Once any variable is defined using const it cannot be reassigned. Variables declared by Const are block scoped and not function scoped as defined by var.
The main drawback with const key word is that it doesn't prevent object from mutability. The properties of the object can be changed even though that object is defined using const. This is called mutability. There is a generalization that any variable that is assigned using const cannot be reassigned again. But when an object is defined using const, its properties can be changed. In that scenario it is better to avoid const to prevent mutability.
Example
In the following example initially the value of property 'country' is "India". But later on the value is changed to England even though the variable is assigned using const.
<html> <body> <script> const person = { "name" : "Suresh", "Country" : "India", "Designation" : "Mayor" } person.Country = "England" document.write(person.Country); </script> </body> </html>
Output
England
Object.freeze()
This method provides immutability. Once any object is frozen, its properties cant be changed.
In the following even though the value of property "country" is changed from"India" to "England", because of immutability the value "India" retains its place.
Example
<html> <body> <script> const person = { "name" : "Suresh", "Country" : "India", "Designation" : "Mayor" } Object.freeze(person); person.Country = "England" document.write(person.Country); </script> </body> </html>
Output
India
- Related Questions & Answers
- What is the difference between Object.seal() and Object.freeze() in JavaScript?
- What is the difference between const int*, const int * const, and int const *?
- Difference Between Static and Const in JavaScript
- What is the difference between #define and const Keyword in C++?
- What is the difference between keywords const and readonly in C#?
- Difference between const int*, const int * const, and int const * in C
- What is the main difference between int.Parse() and Convert.ToInt32 in C#?
- what is the main difference between '=' and '==' operators in javascript?
- Difference between const int*, const int * const, and int const * in C/C++?
- Difference between const char* p, char * const p, and const char * const p in C
- What is difference between int and const int& in C/C++?
- What is the difference between "const" and "val" in Kotlin?
- What is the difference between == and === in JavaScript?
- Difference between #define and const in C
- Difference between #define and const in Arduino