Variable Declaration Not Necessary in JavaScript

In the Chrome Developer Console, it seems as if no variable declaration is necessary. You can type in

a = 5
b = 3
a + b

and it works just fine without let or const…why is this?

And why does JavaScript require variable declarations while something like Python does not??


I do not know Python at all and I tried to google the question and here is what I’ve found:

Regarding the variable declaration in Chrome’s console. Actually, it’s a good question. I’ve also tried to google it and find out what is the actual reason. Here are my findings:

I hope this will help you :slight_smile:

It’s considered bad practice because you’re actually declaring the variables in the global scope, instead of just within the function (or block). It spells trouble when you have the same variable name in different functions. They will all touch that same global variable instead of having their own

The console is useful for trying things out. If you had to use let/const you would need to refresh the console (by refreshing the current browser page) every time you wanted to try something using the same variable names, which is a pain. So you can just declare global variables (which would normally cause warnings in actual strict mode code). The Node REPL is exactly the same.