Taking about the var keyword!๐Ÿ˜€

Taking about the var keyword!๐Ÿ˜€

ยท

2 min read

What are Keywords?

what are keywords to begin with? from my last write-up we know that Keywords are reserved words that are part of the syntax in the programming language.

for example

var a = 'hello';

Here, var is a keyword that denotes that a is a variable.

The var statement declares a variable. Variables are containers for storing information. Creating a variable in JavaScript is called "declaring" a variable

var carName;

After the declaration, the variable is empty (it has no value).


var and let are both used for variable declaration in javascript but the difference between them is that var is function scoped and let is block scoped. Variable declared by let cannot be redeclared and must be declared before use whereas variables declared with var keyword are hoisted.

Why is var still used in JavaScript?

The real advantage of var is its compatibility.

var is probably still used for legacy reasons. It is supported in all versions of JavaScript and it would be a bother to change every example on the internet.

Difference between let and var in JavaScript

The VAR keywordsThe LET keywords
The var is a keyword that is used to declare a variableThe let is also a keyword that is used to declare a variable.
var is an ECMAScript1 feature.let is a feature of ES6.
Syntax -: var name = value;Syntax -: let name = value;
The variables that are defined with var statement have function scope.The variables that are defined with a let statement have a block scope.
We can declare a variable again even if it has been defined previously in the same scope.We cannot declare a variable more than once if we defined that previously in the same scope.
Hoisting is allowed with var.Hoisting is not allowed with let.
Its supported browsers are: Chrome, Internet Explorer, Microsoft Edge, Firefox, Safari, and operates itsIts supported browsers are -: Chrome49, Microsoft Edge12, firefox44, safari11, opera36

Hope you enjoyed your reading, Stay tuned! Peace out โœŒ๐Ÿ˜Ž.

ย