This question already has answers here:
Why can I change a constant object in javascript
(12 answers)
JavaScript const Keyword
(8 answers)
Closed 7 years ago.
Using JavaScript ES6, I am surprised that doing:
const a = {};
a.foo = 'bar';
a.foo = 'car';
Is valid. Why is this? I would have assumed const would mean you cannot change the a empty object and apply new properties. Even further, I would also have assumed you cannot change the value of a property of a once it is set.
Only the variable assignment is constant. Any objects or arrays referenced stay mutable.
const a = {one: 1};
a.three = 3; // this is ok.
a = {two: 2}; // this doesn't work.
What you can do is use Object.freeze:
const a = {one: 1};
Object.freeze(a);
a.three = 3; // silently fails.
// a is still {one: 1} here.
No, const a means you cannot change the value of the variable a. Its value is always the same object; changing properties of an object doesn't make it into a different object.
Using an analogy, I am the same Person whether amadan.jacket = null or amadan.jacket = "Heavy Winter Jacket". amadan is constant.
To make the properties immutable, you would either have to make the properties explicitly readonly by writable: false, or use Object.freeze or Object.seal (differences) to make the entire object immutable.
Related
This question already has answers here:
How do I make a JavaScript variable completely immutable?
(3 answers)
Closed last year.
In javascript, if I create a const array, I can still modify the object the variable points to:
// The const declaration creates a read-only reference to a value.
// It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
const x = [1,2,3];
x.push(4);
console.log(x);
x=55 // But this is illegal and will error
console.log(x);
Is there a way to make the elements in an array immutable as well? Similar to something like const int* const x; in C?
You can use Object.freeze to prevent an object (or array) from being changed:
const x = [1, 2, 3];
Object.freeze(x);
x.push(4); // This will throw an exception
objects frozen with Object.freeze() are made immutable.
Here are the docs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
Example:
const x = [1,2,3];
Object.freeze(x);
This question already has answers here:
Why can I change a constant object in javascript
(12 answers)
JavaScript ES6 `const a = {}` is mutable. Why? [duplicate]
(2 answers)
Closed 1 year ago.
I came across this interesting concept where const behave differently for number, string, Boolean vs Object, arrays.
const num1 = 10
num1 = 20 //throws error
const arr1 = [1,2,3,4]
arr1.push(5) //works
arr1.pop() //works
arr1[2] = 7 //works
arr1 = [2,3] //throws error
For array & objects it allows change in value but throws error in assignment. Is it mean, there is no advantage of declaring object or array as const, as you can change all values anytime?
If anyone can explain, I would like to know, how, behind the scene, const works?
The const declaration creates a read-only reference to a value.
The value it holds is still immutable. Both array and object are holding some values. Those values and properties can be changed but same variable identifier cannot be reassigned.
Here arr1 is an variable identifier
const arr1 = [1,2,3,4]
arr1.push(5) //works // changing the value
arr1.pop() //works //changing the value
arr1[2] = 7 //works // changing the value
Here arr1 got reassigned so it is throwing an error
arr1 = [2,3] //throws error // reassigning the same variable
This question already has answers here:
How to check object is a Set? [duplicate]
(3 answers)
How to reliably check an object is an EcmaScript 6 Map/Set?
(5 answers)
Closed 3 years ago.
Is it possible to determine whether a variable is a Set of not in NodeJS?
That is, this code
let thing1 = new Set([1,2,3])
let thing2 = [1,2,3]
console.log(typeof thing1)
console.log(typeof thing2)
reports that both variables contain an object
object
object
Is there a built-in way to determine if one variable contains a set or not? If not, is there a common heuristic used to determine if a variable is a set or not?
Have you tried instanceof?
let thing1 = new Set([1,2,3])
let thing2 = [1,2,3]
console.log(thing1 instanceof Set)
console.log(thing2 instanceof Set)
This question already has answers here:
Why can I change a constant object in javascript
(12 answers)
Closed 6 years ago.
Why does node.js allow a module (or an object) specified as a constant to be changed?
For example, this is allowed:
const EXPRESS = require('express');
EXPRESS.someProperty = 'some value';
But this is not:
const MYCONST = '123';
MYCONST = '456';
const means that you cannot change the reference itself, not what the reference points to.
const a = { name: 'tom' };
// you cannot change the reference (point a to something else)
a = 5; // this is an error
// but you can change the value stored at that reference with no problem
a.name = 'bob';
const does not make the object to become immutable thus you can change the object itself but you can not assign another object to that reference
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/const
From the docs:
The const declaration creates a read-only reference to a value. It
does not mean the value it holds is immutable, just that the variable
identifier cannot be reassigned. For instance, in case the content is
an object, this means the object itself can still be altered.
This isn't node-specific, it is part of the Javascript spec. The reference EXPRESS is the constant, and when you declare using const you are not allowed to reassign the reference.
const EXPRESS = require('express');
EXPRESS = 'something else';
would also fail
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 6 years ago.
I have two variable, attribute (e.g. "type") and value (e.g. "car"). I want to make an object where the key is the attribute (and the value is the value). Like this: {"type": "car"}.
When I do
let obj = { attribute: value }
I get
> {"attribute": "car"}
This is easy with two lines, as I can just
let obj = {};
obj[attribute] = value;
However, I'm wondering if there is a clean way of doing this in one line (since I'm a former Rubyist and I like making things clean and precise)?
Computed property names, starting from ES2015 aka ES6.
let a = "type", b = "car";
console.log({[a]: b});