How to create and check read-only properties in JavaScript objects using Object.defineProperty()

Srijan } Author Pic
SrijanMar 01, 2025 - JavaScript
How to create and check read-only properties in JavaScript objects using Object.defineProperty() - Reacted Node

Photo: Engin Akyurt/Pexels

Introduction

In JavaScript, objects are inherently mutable, which means that the properties of an object can be modified even after the object has been created. While this flexibility is often desirable, there are scenarios where you must prevent accidental or intentional changes to specific properties. Read-only properties are essential in this context, and Object.defineProperty() is your key tool.

This article will explain how to create and check read-only properties in JavaScript objects, ensuring better data integrity and security.

Table of Contents

Creating a Read-Only Property with Object.defineProperty()

Object.defineProperty() allows you to control the behavior of object properties. By setting the writable descriptor attribute to false, you can prevent modifications to a property while still allowing it to be accessed.

Syntax

1Object.defineProperty(object, propertyName, descriptor);

Example

1const myObject = {};
2
3Object.defineProperty(myObject, "id", {
4 value: 123,
5 writable: false, // Makes id read-only property
6 enumerable: true, // Optional: Makes the property appear in loops
7 configurable: false, // Optional: Prevents further redefinition or deletion
8});
9
10console.log(myObject.id); // Output: 123
11
12myObject.id = 456; // Attempting to modify the property
13console.log(myObject.id); // Output: 123 (remains unchanged in strict mode. In non-strict mode, the assignment is silently ignored.)

Checking If a Property Is Read-Only

You can use Object.getOwnPropertyDescriptor() to retrieve the descriptor of a property and check its writable attribute.

1const descriptor = Object.getOwnPropertyDescriptor(myObject, "id");
2
3if (descriptor && descriptor.writable === false) {
4 console.log('The "id" property is read-only.');
5} else {
6 console.log('The "id" property is not read-only.');
7}

Enforcing Read-Only Properties in Strict Mode

In non-strict mode, failing to write to a non-writable property will silently fail. In strict mode , a TypeError will be thrown, which is preferred for debugging.

1"use strict";
2
3const settings = {};
4
5Object.defineProperty(settings, "version", {
6 value: "1.0.0",
7 writable: false,
8 enumerable: true,
9 configurable: false,
10});
11
12console.log(settings.version); // Output: "1.0.0"
13
14settings.version = "2.0.0"; // TypeError: Cannot assign to read-only property 'version'

Summary

In JavaScript, ensuring data integrity and preventing unintended modifications is crucial. The Object.defineProperty() method provides a powerful mechanism to create read-only properties by setting the writable attribute to false. This technique allows developers to lock down specific properties, safeguarding critical data and enhancing code robustness. Furthermore, Object.getOwnPropertyDescriptor() enables the verification of a read-only property. By leveraging these tools, developers can build more reliable and secure applications, effectively managing object properties and controlling data mutability.