Deciding between let, const and var in babylon

I noticed that all playground examples I found tend to always use the var keyword for variable definitions.
I feel like in other fields where javascript/typescript is uses there has been a strong movement over the past years to only use let and const instead, which I agree with.
So far I haven’t really run into any issues using let and const, but are there reasons why you would want to use var instead with babylon ?

There is no real reason not to use let and const, especially for webgl applications, since all browsers supporting webgl (including IE11) support let and const.

Why do people do that? because it is working, and they know how to use it. :slight_smile:

If you want my personal opinion - use let and const as much as you want (and only use them). var is outdated, but so is underscore or jquery. and yet, people love using them!

2 Likes

Many playgrounds were created before let or const were available or not well know and changes and additions to these playgrounds tend to follow the pattern established in them.

Personally I still use var out of (bad?) habit my fingers hit v, a, r almost automatically when creating a variable. The last time I used let regularly was in the days of BBC BASIC.

As @RaananW suggested get into the habit of using let and const just be aware of the different scoping rules.

2 Likes

We solved this in our team and in our BABYLON based product as:

“You should have a reason (as a sentence in your mind) not to declare something as const”.

And this reason should be meaningful.
To answer the question - I don’t think there is a reason to use var in BABYLON

1 Like

I think (not sure) that as long as the transpilation target is ES5, everything remains converted into “var” anyway.

3 Likes

Not to mention no real use for semicolons as well save for very specific situations.

But I would argue there are very specific cases where var couldd be ideal. Let’s say you have a loop that for some odd reason you want to overwrite the variable you are sampling at an indeterminate point, a let would assign a new reservation on the ram I belive as opposed to the var that would be reserved once and overwritten? I don’t know im just think fringe cases.

I do know they have different scope usages though.

1 Like

Does let ever run any faster or is it just a stricter scoping thing? Ive started using const for some code clarity. People have just been typing var for 20 years so its a tough sell to change. I haven’t really taken to any of the new ES6 stuff, it doesn’t seem to increase performance at all. I think i just need some practice with it. Is anyone using promises to great effect?

1 Like

It’s a scoping thing only. It may improve your code safety or sustainability. I don’t think it has something to do with the execution speed (at our perception level).

3 Likes

scopeing only. Your thread still executes at the same speed.

2 Likes

Thank you for all the great answers.
I’ll happily keep on using const and let then!

2 Likes

Just don’t use var :smiley:

FWIW: in daily work I mainly use TypeScript setup with eslint in vscode to autofix on save. This means that it will auto-magically change var to let and let to const if that variable is never mutated - every time I save. This way I can focus on functionality and never have to worry about let vs. const :stuck_out_tongue:

2 Likes