Default Values of Property Descriptors in JavaScript
javascript·@ghasemkiani·
0.000 HBDDefault Values of Property Descriptors in JavaScript
When you create a property on an object using the assignment operator or the object literal format, the JavaScript engine creates a property that is **writable**, **configurable**, and **enumerable**, by default. ```javascript let ali = {}; ali.age = 19; let susan = { age: 14, }; console.log(Object.getOwnPropertyDescriptor(ali, "age")); console.log(Object.getOwnPropertyDescriptor(susan, "age")); ``` This is what we get from the last two lines of the code above: ```javascript { value: 19, writable: true, enumerable: true, configurable: true, } { value: 14, writable: true, enumerable: true, configurable: true, } ``` You might think that the same happens with `Object.defineProperty` and `Object.defineProperties`, too. But this is not the case. The default value of the `writable`, `enumerable`, and `configurable` attributes, when using these functions, is `false`. If you need the flag to be set, you have to provide the `true` value yourself. Here is an example to illustrate this fact: ```javascript let susan = {}; Object.defineProperty(susan, "age", { value: 14, }); console.log(Object.getOwnPropertyDescriptor(susan, "age")); ``` This is the result from this code: ```javascript { value: 14, writable: false, enumerable: false, configurable: false, } ``` As you see, the `writable`, `enumerable`, and `configurable` attributes default to a value of `false`. --- ## Related Posts * [Object Creation and Manipulation Functions in JavaScript](https://steemit.com/javascript/@ghasemkiani/object-creation-and-manipulation-functions-in-javascript) * [Property Descriptors in JavaScript](https://steemit.com/javascript/@ghasemkiani/property-descriptors-in-javascript) * [Getting and Setting Property Descriptors in JavaScript (Part 1)](https://steemit.com/javascript/@ghasemkiani/getting-and-setting-property-descriptors-in-javascript-part-1) * [Getting and Setting Property Descriptors in JavaScript (Part 2)](https://steemit.com/javascript/@ghasemkiani/getting-and-setting-property-descriptors-in-javascript-part-2) * [Getting and Setting Property Descriptors in JavaScript (Part 3)](https://steemit.com/javascript/@ghasemkiani/getting-and-setting-property-descriptors-in-javascript-part-3)
👍 ghasemkiani, vashkevich37, kinggs, zahidsun, shahin24, appreciator, sam1210, vexedkiller007, meme-machine, pornstarshourly, nurhasib, techshahinpro, amitraj, resteemedvote, syibran, millionairkhan, bestmdfisal01, tryptameme, cryptoeagle, molometer, unnun, amok-wilde, satyamsharma, scipio, steemblake, lunahod, ansari222, benziman, jubayer1, jfernandez, lanmower, relevantsource, mobilser, jamimo,