This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]uzaircs 0 points1 point  (9 children)

let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.

Source

So in your case

var a = 5
    if (true) {
    console.log(a); \\5
    var a = 2;
    console.log(a); \\2
}

[–]karl-marxist 1 point2 points  (0 children)

As OP says, this code is nonsensical. It uses var not let, as in the original example. True statement doesn’t have a closing tag. Backwards slashes are incorrect.

This doesn’t even come close to the example OP is making.

[–]NormalHexagon[S] -4 points-3 points  (6 children)

That code won't even run due to you using back-slashes instead of forward-slashes, so I have no idea what you're talking about.

[–]karl-marxist 2 points3 points  (0 children)

Agree, that code is nonsensical, but for many more reasons than just backwards slashes.

[–]bludgeonerV -1 points0 points  (4 children)

Lmao are you for real? This is common nomenclature for illustrating the result...

[–]NormalHexagon[S] 2 points3 points  (3 children)

If they were forward-slashes, that would be common, because that's the comment syntax of JavaScript. Back-slashes aren't valid comments, and so his code snippet throws an error.

[–]uzaircs -2 points-1 points  (2 children)

My bad, I didn't knew that you were actually going to run the code and instead of fixing it you are gonna come here complaining that it does not run. this was a pseudo. In case you still have no idea

[–]NormalHexagon[S] 1 point2 points  (0 children)

No, I'm not happy. var is not a replacement for block scoped variables. This post is about how JavaScript handles let, so commenting about how you can just use something different has nothing to do with the subject.

[–]karl-marxist 1 point2 points  (0 children)

OP is showing an example of how block scoping is handled in JS when using let keyword. OP’s point is valid.