JavaScript Tutorials

Overview

In JavaScript, there are numerous ways to protect an Object from being changed.

Using the const Keyword

With const you can not re-assign the object, but you can still change the value of a property, delete a property or create a new property.

// create object
const person = {
    firstName: "Johnny",
    lastName: "Shay"
};

JavaScript Object.preventExtensions()

The Object.preventExtensions() method prevents adding properties to an object.

// create object
const person = {
    firstName: "Johnny",
    lastName: "Shay"
};

// prevent extensions
Object.preventExtensions(person);

// test error
let text = "";
try {
    person.age = 12;
}
catch (err) {
    text = err;
}

document.getElementById("demo").innerHTML = text;

Error:

TypeError: Cannot add property age, object is not extensible

The Object.preventExtensions() method will also prevent items from being added to an array within an object.

// create object
const cars = ["Lamborghini", "Ferrari", "Maserati"];

// prevent extensions
Object.preventExtensions(cars);

// test error
let text = "";
try {
    cars.push("Alfa Romeo");
}
catch (err) {
    text = err;
}

document.getElementById("demo").innerHTML = text;

Error:

TypeError: Cannot add property 3, object is not extensible

JavaScript Object.isExtensible()

The Object.isExtensible() method is used to check if an object is extensible. It will return “true” if an object is extensible, and “false” if it is not.

// create object
const person = {
    firstName: "Johnny",
    lastName: "Shay"
};

// prevent extensions
Object.preventExtensions(person);

// test
let answer =  Object.isExtensible(person)
document.getElementById("demo").innerHTML = answer; // will return false

JavaScript Object.seal()

  • Prevents additions or deletions of new properties
  • Makes existing properties non-configurable
  • Can be used to check if an object is sealed
"use strict"

// create object
const person = {
    firstName: "Johnny",
    lastName: "Shay"
};

// seal object
Object.seal(person)

// test
let text = "";
try {
    delete person.lastName;
    text = Object.values(person);
}
catch (err) {
    text = err;
}

document.getElementById("demo").innerHTML = text;

Error:

TypeError: Cannot delete property 'lastName' of #

Note

The Object.seal() method will fail silently in non-strict mode and throw a TypeError in strict mode.

JavaScript Object.isSealed()

The Object.isSealed() method is used to check if an object is sealed. It will return “true” if an object is sealed, and “false” if it is not.

// create object
const person = {
    firstName: "Johnny",
    lastName: "Shay"
};

// seal object
Object.seal(person);

// test
let answer =  Object.isSealed(person)
document.getElementById("demo").innerHTML = answer; // will return true

JavaScript Object.freeze()

The Object.freeze() method prevents any changes to an object, making the object read only. No modification, addition or deletion of properties are allowed.

"use strict"

// create object
const person = {
    firstName: "Johnny",
    lastName: "Shay"
};

// freeze object
Object.freeze(person)

// test
let text = "";
try {
    person.lastName = "Chicken";
    text = Object.values(person);
}
catch (err) {
    text = err;
}

document.getElementById("demo").innerHTML = text;

Error:

TypeError: Cannot assign to read only property 'lastName' of object '#'

Note

The Object.freeze() method will fail silently in non-strict mode and throw a TypeError in strict mode.

JavaScript Object.isFrozen()

The Object.isFrozen() method is used to check if an object is frozen. It will return “true” if an object is frozen, and “false” if it is not.

// create object
const person = {
    firstName: "Johnny",
    lastName: "Shay"
};

// freeze object
Object.freeze(person);

// test
let answer = Object.isFrozen(person);
document.getElementById("demo").innerHTML = answer; // will return true

JavaScript Notes:

  • When using JavaScript, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
  • JavaScript is a case-sensitive language; firstName is NOT the same as firstname
  • JavaScript variables are case sensitive (x is not the same as X)
  • Arrays count starting from zero NOT one; so item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on
  • JavaScript variables must begin with a letter, $, or _

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.