Why can you modify an object created with const?

preview_player
Показать описание
In JavaScript, when you declare variables with const, it is assumed that "the variable cannot change". This is very clearly seen with variables that have primitive data types like strings, numbers, booleans.

But when you declare a variable with an object type (a reference data type), what you notice is that such variables can be modified even when you use const. Why is this the case? This is what I explain in this video with examples.

Resources:

_____

🌟 About Me:
Dillion Megida, creator of DEEECODE, is a Software Engineer, Developer Advocate and a Content Creator passionate about simplifying topics around Tech via articles and videos.

_____

⚡️ Connect with me:
Рекомендации по теме
Комментарии
Автор

i had the same doubt and landed in your video. Also, no only it allows to modify properties but also add new ones and why use const in first case. Thanks man !

walterl
Автор

However, if data is a proxied, frozen, or pre-processed object, or if it comes from a library or framework that limits property extension (e.g., an immutable object), then simply adding properties ( data.language = "javascript" ) might not "stick" in the way you expect. In that case you'll have to use the spread operator and create a new object ( ...data, language: "javascript" ).

Midwayne
Автор

Keep posting such conceptual videos on JavaScript because ot is helping to all the developers from Angular, React or Vue.

ajiteshmishra
Автор

this referenced type data types stores the reference of that value ( Mem Address simply) Instead content , That Mem Address contains the Content, so here you are trying to change content of const variable. That's why you are able to modify eventhow you declared using const

Ramesh-js
Автор

Very apt explanation! It reminds me of pointers in C

kushyzee
welcome to shbcf.ru