javascript questions
Hey, i've just started properly diving into javascript and have a few questions.
1.) What are the less obvious differences between
let
, const
and var
? I'm aware let
is used to declare generic variables and const
is used for constant variables that never change etc. however i've heard you shouldn't use var
but i've also heard it has it's uses in some situations?
2.) Why do functions such as .length
not require double parenthesis after it, but functions such as .toUpperCase()
do?
3.) What do @param
's mean in comments? I've seen them in a few files.
I'd appreciate any help. Thank you in advance.5 Replies
var
should be considered deprecated and never used, imho. What var
does is take the declaration and hoist it to the top of the script, then wait for the assignment later on.
gets translated to
It can cause weird bugs when you forget that you used a variable name somewhere
.length
isn't a function, it's a property (a variable on an object). Its value is (as far as you should worry about, ignoring implementation here) just always set. .toUpperCase()
is a method (a function on an object), which requires some calculation to do, so you have to call it as a function
@param
is a jsdoc thing, I think? it's not part of the regular ecmascript spec afaik
as for the uses for var... I wouldn't consider them, especially if you're starting out. Needing to use var is usually an excuse to not be slightly more verbose and clear, and it's just a Bad Idea™️ in so many cases that having to defend it for the couple of valid use cases you might find just isn't worth the trouble
oh, and one other thing is that var
always declares in the global scope unless you use it in a function, where let
and const
declare in local scope. Local scope is much preferable in almost all situations, and if you really want something to be declared globally, you should declare it globally yourself.
One thing you can do with var
that's occasionally convenient, is declare a variable inside of an if statement and it'll work outside of that statement:
as opposed to:
You'd have to write it like this:
or, depending on the circumstance,
or, more likely for something super simple like this:
but usually you'd have more complicated logic in the branches.ah i see, thank you for the explanations, it makes more sense now
and i assume it'd say "error: test is undefined" in that second example as you'd have to use
return
to pull the value out of the if statement right?you can't use return in an if statement, only in a function
it's one of those cases where a minimal example can be a bit confusing, normally you'd have a half-dozen or more lines in each block, or at least in one of them. You'd compose a value, and then assign it to
test
for example, or do more checks.
this version:
is just nonsensical, you have to use one of the later versions. It's just there to demonstrate how it doesn't work, where using var
would work
but again, if you're learning javascript now, just pretend var
doesn't exist. Don't ever use it.oh okay got it, thank you
yes, it is a jsdoc thing
with vscode, using that, you can have some degree of typechecking
but the intended use is to document the arguments of a function