DEV Community

Dante Calderón
Dante Calderón

Posted on

Can You write a complex program only using const variables in javascript?

What do you think?

Top comments (5)

Collapse
 
macsikora profile image
Pragmatic Maciej

There are languages where you cannot re-assign (what const is exactly about) but even you cannot mutate (what const is not about), and still you can write any kind of software in these languages.

And in JS perspective, I didn't use let for about 2 years and I was working with really big web system. And its not like I am against let, in my opinion we can use only let, only const or mix them and its ok. Mixing them is how it was design, you want to re-assign use let, you don't use const.

BTW. TypeScript differentiates these concepts, as for const the type narrowing is mor strict, and it means that if you have let the type can change, if you have const it can't change.

Collapse
 
anwar_nairi profile image
Anwar

At my (yesterday last day of) job, we wrote an interactive, zoom/pan/drag SVG engine, using Typescript. 0 var, only const or let (and classes), to renew our previous, var full engine we wrote in JS.

Not to mention errors drastically dropped (500 lines long unminifed).

So yes it is possible!

Collapse
 
ozzyogkush profile image
Derek Rosenzweig

Depends. If you ever need to re-assign a value to a variable or use a for ... of or for ... in loop , the answer is no. If you do not, then the answer is yes. Sometimes it's unavoidable, and even if it were avoidable, that may not necessarily be the right move.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Yes absolutely

Collapse
 
pris_stratton profile image
pris stratton

I think so. You can still mutate objects assigned to consts, assign them functions and use recursion to avoid loops.