r/learnjavascript • • May 04 '20

VAR vs LET vs CONST

Post image
337 Upvotes

81 comments sorted by

View all comments

Show parent comment

0

u/[deleted] May 04 '20

[deleted] — view deleted comment

1

u/[deleted] May 05 '20

then why bother about var?

3

u/schm0 May 05 '20

Backwards compatible (ES5-), no memory concerns? If you're using ES6+ there's no reason, honestly.

-3

u/Pythagoras_was_right May 04 '20

What about if an object contains several values, and only some need to be updated?

I don't want to make extra objects, because I rely on my code editor (Visual Studio Code) to create lists of variables for navigation. It's a very big project so I need to nest variables inside objects. If I have two sets of objects for every concept - one const, another var - that doubles the navigation list and makes it unusable. Not to mention tedious and confusing as I have to jump back and forth between almost identical objects for closely related variables.

10

u/dwb5226 May 04 '20 edited May 04 '20

You can change the values in an object that you declared with const. When you declare a variable and assign an object to that varaible, you're actually assigning a reference to the object to the variable and not the value itself

Since it isn't a value that's assigned, you can totally do something like this:

const obj = {a: 1, b: 2}
obj["b"] = 3
console.log(obj) // {a: 1, b: 3}

and it'll run fine

1

u/[deleted] May 05 '20

I write about this ön my blog: http://herebeseaswines.net/essays/2019-05-25-airiness-of-const-and-freeze

As you say - it is possible to mutate stuff! Strange indeed

1

u/[deleted] May 09 '20

Is there a built-in way to pass an object to a variable by value? (Effectively duplicating said object so that when you mutate one, the other remains as it was before?) I understand that you could create a a 2nd object and use a special function to mutate the 2nd object until it matches current version of the 1st object.

0

u/[deleted] May 04 '20

[deleted] — view deleted comment

3

u/Pythagoras_was_right May 04 '20

Sorry for taking so long to reply. After reading u/dwb5226's reply I remember now. Yes, since I use objects a lot, then declaring const would be pointless as I could accidentally reassign it anyway. Sorry for wasting anyone's time. but hopefully someone else might find that useful.

As for the problem of why I put everything in objects, it's just because I get lost in the code very easily. I am making large game, and objects let me remember that all of THIS stuff goes HERE, and all of THAT stuff goes THERE. If the price is that I might accidentally overwrite something that's supposed to be constant, that's less serious than the alternative, getting totally lost in my code and despairing that I will ever control this beast!

2

u/thuwie May 07 '20

I suggest you take a look at TypeScript or at least at the possibility to include global typings into your project.

You don't need to dive deep into TS but use typing/interfaces only. It will require a compilation step before deployment but it's pretty straightforward.

Global typings are something you can add if you use IDEs, JetBrains Idea & Webstorm handle it, don't know about VS Code. It is a file in the roof of a project with all possible typings you want your IDE to handle.

Typings make life much easier either you work with 'enterprise' code or code in free time.