asuming that a "const" is constant, I wouldn't have thought that this is possible:
const obj = { key1: value1, key2: value2 }; obj["key3"] = value3; obj.key4 = value4; Can somebody explain it to me?
In your case, you aren't actually changing obj. You're just changing a property of obj, so it isn't considered like you're trying to change a constant.
Only doing this is problematic:
const obj = { key1: value1, key2: value2 }; obj = 5 const, look into Object.freeze: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…obj = { a: 1, b: { c: 2 } } then obj.a = 3 results in an error, but obj.b.c = 3 is still allowed. (only obj is frozen, obj.b is not)
const"variable" is a variable that cannot be re-assigned to another value once created. By mutating the object the variable still holds the same reference, so the variable did not change.objto be immutablevalue1-value4are not defined. Changing them to strings resolves this. But the most likely reason for the downvotes is probably the lack of research. If you searched "JavaScript why can I change a const" using any search engine, you would probably find an answer. I personally don't find the question downvote worthy, but I assume others might disagree.