In JavaScript, you can reassign values to variables you declared with let or var.
I used to reassign values a lot. But as I got better with JavaSc...
For further actions, you may consider blocking this person and/or reporting abuse
Even easier to read?
@miguelqueiroz - I was thinking the same. Admittedly the function wrapper above is pointless
That's my point. But its funny to know your hair is sort of undefined for Saturday or sundays ehe..
Bad hair days! 😝
Ooh! You're completely right there. I should have came up with a better example 😅.
or to allow rapid visualisation of constants:
no undefined, no function call cost
Good article @zellwk . Another useful habit to cultivate is not allowing your functions to modify anything that wasn't created within it (or at the least, passed in as an argument). That way you won't have to worry too much about mistakenly reassigning something else.
Yup. Pure functions FTW.
This is what I tell people when they argue against using
const
("it's not really immutable"). When usingconst
is the default, we naturally think in ways that result in better code.Most functions have a structure like this:
When we are allowed to reassign, these "concerns" easily get intertwined and a clear separation is not visible. It also becomes harder to extract out meaningful function out for reuse.
With no reassignment the code looks more like:
There is a clear separation of concern and it is easier to refactor.
WTH ? I strongly believe this promotes stupidity amongst programmers. It's verry natural for a function to be able to access global variables, both for read and write operations. It's your job as a programmer to fully understand the function you're trying to change, which variables are local and which are global. Not to mention that you could have naming convensions like "all global variables start with g_". And on top of that, it's a good practice to comment your code, for both you and others. Simply placing a comment like "// here we set value X in global variable Y" makes things crystal clear.
If we would use each variable only once, we would need to work with far more variables and our code would be alot bigger and alot messier. Not to mention we would increase memory size.
A simple example i can think of, often found in my codes, is something like this (pseudocode here, i work mostly in PHP):
Basically, if i would need 5 different queries, returning large amounts of data, but i only need that data for a verry short amount of time, why on earth would i want to create separate variables to load my memory more than needed ?
Optimization can often lead to code that is harder to read, test and maintain. You are right saying that is the task of an programmer to understand the code. But I would not like to work in such code. Moreover in times of huge in-memory-databases, memory is not as expensive as programmers maintaining and extending the code.
Just noticed you may want to rewrite this bit:
The
const
keyword creates constants, not variables.They're not really "constants" in a strict sense because, even though you can't reassign, you can modify the referenced value (if it's an object). For instance:
I would totally not use a function but instead an object like:
Hair={"monday":"bangs",
"Tuesday":"braids"};
and so on...
return Hair[today];
the real problem is not reassigning values, the real problem is global scoped variables.
javascript is really nasty. it was not chosen because it is the best.