Why shouldn't we use var in JavaScript?

Var behaves very differently.

If you have some knowledge about scoping, you would know that let and const declared variables are block scoped – meaning – they are not accessible globally but restricted to the function they are declared in.

Whereas, VAR doesn’t really remain restricted to a local scope. It’s a global variable.

Also, Var does hoisting as well. This would make solving bugs really confusing.

You can re-declare var and it again becomes very confusing as we don’t really get to know if a variable wasn’t to be declared again.

These are reasons why VAR is not used commonly now. It is still fine to use if you know your stuff but generally, avoiding using it is the best practice.

I don’t believe in writing super long articles but to-the-point articles that answer questions. If this article helped you out, please go through out blog section, there are plenty of gem of articles waiting for you!

Leave a Reply

Your email address will not be published.