- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to disallow altering of object variables in JavaScript?
Use the concept of freeze() from JavaScript to disallow adding new properties to an object, altering of object properties, etc.
Following is the code wherein we are changing the value but the previous value still remains since we cannot alter properties using freeze() −
Example
const canNotChangeTheFieldValueAfterFreeze = {value1 : 10,value2: 20 }; Object.freeze(canNotChangeTheFieldValueAfterFreeze); canNotChangeTheFieldValueAfterFreeze.value = 100; console.log("After changing the field value1 from 10 to 100 ="+canNotChangeTheFieldValueAfterFreeze.value1);
To run the above program, you need to use the following command −
node fileName.js.
Output
Here, my file name is demo97.js. This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo97.js After changing the field value1 from 10 to 100 =10
- Related Articles
- How to extract variables of an S4 object in R?
- How to disallow resizing component with GridBagLayout in Java
- How to declare variables in JavaScript?
- How to name variables in JavaScript?
- How to declare global Variables in JavaScript?
- How to declare String Variables in JavaScript?
- How to declare boolean variables in JavaScript?
- How to use Global Variables in JavaScript?
- How to use Static Variables in JavaScript?
- How to minimize the use of global variables in JavaScript?
- How to pass JavaScript variables to PHP?
- How to use JavaScript variables in jQuery selectors?
- How to concatenate multiple string variables in JavaScript?
- How to swap variables with destructuring in JavaScript?
- How to declare Block-Scoped Variables in JavaScript?

Advertisements