TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript Object freeze() Method

JavaScript Object freeze() Method with example on javascript, object, javascript assign(), create(), defineProperties(), defineProperty(), entries(), freeze(), javascript tutorial, javascript array etc.

<< Back to JAVASCRIPT

JavaScript Object.freeze() Method

The Object.freeze() method freezes an object that prevents new properties from being added to it. This method prevents the modification of existing property, attributes, and values.

Syntax:

Object.freeze(obj)

Parameter

Obj: The object to freeze.

Return value:

This method returns the object that was passed to the function.

Browser Support:

Chrome 45.0
Edge 12.0
Firefox 32.0
Opera No

Example 1

const object1 = {
  property1: 22
};
const object2 = Object.freeze(object1);
object2.property1 = 33;
// Throws an error in strict mode
console.log(object2.property1);

Output:

22

Example 2

const obj1 = { property1: 'freeze'};
const obj2 = Object.freeze(obj1);
obj2.property1 = 'new_data';
  console.log(obj2.property1);

Output:

" freeze "

Example 3

 var obj = { prop: function() {}, name: 'charry' };
        console.log(obj);
        obj.name = 'karri';
        delete obj.prop;
        console.log(obj);
        var o = Object.freeze(obj);
        obj.name = 'chris';
        console.log(obj);

Output:

[object Object] {
 name: "charry",
  prop: function() {}
}
[object Object] {
 name: "karri"
}
[object Object] {
 name: "karri"
}

Next TopicJavaScript Objects




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf