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
Jochem
Jochem4w ago
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.
console.log(test);
var test = 20;
console.log(test);
var test = 20;
gets translated to
var test;
console.log(test);
test = 20;
var test;
console.log(test);
test = 20;
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:
if (someCondition) {
var test = 1;
} else {
var test = 2;
}
console.log(test); //1 or 2, depending on someCondition
if (someCondition) {
var test = 1;
} else {
var test = 2;
}
console.log(test); //1 or 2, depending on someCondition
as opposed to:
if (someCondition) {
let test = 1;
} else {
let test = 2;
}
console.log(test); //error: test is undefined
if (someCondition) {
let test = 1;
} else {
let test = 2;
}
console.log(test); //error: test is undefined
You'd have to write it like this:
let test;
if (someCondition) {
test = 1;
} else {
test = 2;
}
console.log(test)
let test;
if (someCondition) {
test = 1;
} else {
test = 2;
}
console.log(test)
or, depending on the circumstance,
let test = 2;
if (someCondition) {
test = 1;
}
console.log(test)
let test = 2;
if (someCondition) {
test = 1;
}
console.log(test)
or, more likely for something super simple like this:
let test = someCondition?1:2;
let test = someCondition?1:2;
but usually you'd have more complicated logic in the branches.
snxxwyy
snxxwyy4w ago
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?
Jochem
Jochem4w ago
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:
if (someCondition) {
let test = 1;
} else {
let test = 2;
}
console.log(test); //error: test is undefined
if (someCondition) {
let test = 1;
} else {
let test = 2;
}
console.log(test); //error: test is undefined
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.
snxxwyy
snxxwyy4w ago
oh okay got it, thank you
ἔρως
ἔρως4w ago
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
Want results from more Discord servers?
Add your server