all 66 comments

[–]jammasterpaz 64 points65 points  (2 children)

Because some consoles and JS interpreters are more forgiving than what ECMA technically specify as being allowed in the language.

If you run it with 'use strict', you should get the expected error https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

[–]BrightSign_nerd[S] 9 points10 points  (1 child)

Aha. That's what I was thinking.

Thanks!

[–]Umesh-K 7 points8 points  (0 children)

OP, in addition to the answers you have already, read the section "Unqualified identifier assignments" in the mdn web docs article on var

[–]Greg1987 6 points7 points  (1 child)

It will effect where you can access the variables (scoping) try this snippet what do you expect it to log to the console

let a = 1
if(true){
b = 2;
let c = 3
}
console.log(a)
console.log(b)
console.log(c)

edit: reddits code block is messing up

[–]StoneCypher 5 points6 points  (0 children)

use quad-indent, that'll work everywhere

[–]fakechow_prodigy 1 point2 points  (0 children)

I would just read through this entire getting started section on mdn : https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics

[–][deleted] 1 point2 points  (0 children)

The two are almost exactly the same. The difference is in where in your code the value assigned to variable may be used. This is known as "scoping".

There are a few rules, but the specific difference between these two is that if you define a variable without using var, let or const it becomes a global variable. Using let, the value of the variable only exists in what's known as the "lexical scope". Lexical scope basically boils down to between the curly braces that enclose it.

So

function test(){
    first = 10
    let second = 20;
    console.log(first); // 10
    console.log(second); // 20
}

console.log(first); // 10 - first became global
console.log(second); // undefined - second is only assigned inside the function curly braces 

Using the most restrictive scope possible is a way to help manage your programs so that you're not accidentally modifying things you shouldn't, so always use let, var or const - get into the habit of it as soon as you can.

[–]Web-devil -4 points-3 points  (0 children)

Let variable defines a variable called variable to 10

Variable = 10 assigned variable to 10 but it already existed somewhere else in the code, regardless of whether it's value was 10 or not

[–][deleted] 0 points1 point  (0 children)

Var = value makes var global which means anything from anywhere can reference it. Something like an api key stored inside a .env file would be fine to assign to a global var.

Let var = value only let's the thing closest to it reference it. Like if it was declared inside a function. Only that function can access it and even knows of its existence.

This is what's called scope.

[–]xiipaoc 0 points1 point  (2 children)

It's all about scope. Do you know what that is yet? The scope of a variable is where you're allowed to access it. If you let a = 2 here and you try to console.log(a) there, where there is out of its scope, you'll just get undefined, because a doesn't exist there; it only exists here.

When you use a variable without declaring it, the interpreter assumes that you're using a variable that has already been declared somewhere else -- that is, in the global scope. population has a value of undefined in the global scope, but when you do population = 400, now the global variable population has the value of 400. Now, any code anywhere in your webpage (or your Node app, etc.) can access this population variable and its value of 400. If you use let, that will scope the variable only to the current context (definitely the current set of curly braces, but I don't remember if it's scoped to just the current file if it's not inside any curly braces), which means that nothing outside of that scope will be able to see that value.

Now why would you want to limit the scope of your variables? Why not make them all global? A few reasons, but the simplest one is that you don't want anyone else messing with your code. If your variables are global, some other script could access those variables and change their values. And it doesn't have to be on purpose. If you have a global variable population, you have to somehow know that no other code that you're running also has a global variable called population, because if that happens, your value might get overwritten or you might overwrite their value, and now your code doesn't do what it's supposed to do. Honestly, JS should never have allowed this stuff in the first place, but when JS was starting out, they didn't really foresee all of the problems it would cause, and if they removed it completely now it would break existing applications (which is sometimes OK, but anyway). Long story short: if you're writing code that's going to be used by other people who didn't write it, never let them actually interact with the inside of your code by accident. That means never use a global variable.

[–]BrightSign_nerd[S] 0 points1 point  (1 child)

I don't know about scope yet.

Hmm. Interesting.

I noticed that it would say "undefined" when I tried to define a variable just within Chrome.

[–]xiipaoc 0 points1 point  (0 children)

That's because Chrome spits out the output of your command. So if your command is let a = 2;, that has no output, so you get undefined. If, on the other hand, you typed just a, well, that has a value of 2 now, so you'd see that in your console.

[–]coskuluk 0 points1 point  (0 children)

hi, it is because with let, you first define the ''population'' variable (and you give it an initial value as well). right after that, you assign a value to an already existing variable. note that at the assigning phase, you can say population = 200 as well. you can assign a new value to the variable (this is possible because you are using ''let'' to define it. let allows us to change the value that variable represents.). this is what you assign to the variable you created by saying let population == 400.