Skip to content

JavaScript Property Descriptors

An explanation of property descriptors and what they are useful for

Any object in JavaScript has a set of properties, and each of these properties has a descriptor.

This is an object that defines a property behavior and own properties.

Many Object static methods interact with it. Those methods include:

Here is an example of a property descriptor object:

{
  value: 'Something'
}

This is the simplest one. value is the property value, in a key-value definition. This key is defined as the object key, when you define this property in an object:

{
  breed: {
    value: 'Siberian Husky'
  }
}

Example:

const animal = {}
const dog = Object.create(animal, {
  breed: {
    value: 'Siberian Husky'
  }
});
console.log(dog.breed) //'Siberian Husky'

You can pass additional properties to define each different object property:

writable, configurable and enumerable set the behavior of that property. They have a boolean value, and by default those are all false.

Example:

const animal = {}
const dog = Object.create(animal, {
  breed: {
    value: 'Siberian Husky',
    writable: false
  }
});
console.log(dog.breed) //'Siberian Husky'
dog.breed = 'Pug' //TypeError: Cannot assign to read only property 'breed' of object '#<Object>'

→ Get my JavaScript Beginner's Handbook

I wrote 21 books to help you become a better developer:

  • HTML Handbook
  • Next.js Pages Router Handbook
  • Alpine.js Handbook
  • HTMX Handbook
  • TypeScript Handbook
  • React Handbook
  • SQL Handbook
  • Git Cheat Sheet
  • Laravel Handbook
  • Express Handbook
  • Swift Handbook
  • Go Handbook
  • PHP Handbook
  • Python Handbook
  • Linux Commands Handbook
  • C Handbook
  • JavaScript Handbook
  • Svelte Handbook
  • CSS Handbook
  • Node.js Handbook
  • Vue Handbook
...download them all now!

Related posts that talk about js: