Default Values of Property Descriptors in JavaScript

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@ghasemkiani·
0.000 HBD
Default 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)
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,