HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Before ES6 there was only one way of defining your variables: with the var keyword. If you did not define them, they would be assigned to the global object. Unless you were in strict mode, then you would get an error if your variables were undefined
Before ES6 there was only one way of defining your variables: with the
var
keyword. If you did not define them, they would be assigned to the
global object. Unless you were in strict mode, then you would get an error if
your variables were undefined.
Now, with ES6, there are three ways of defining your variables:
var
, let
, and const
.
var x = 5.6;
If you use var
outside of a function, it belongs to the global scope.
If you use var
inside of a function, it belongs to that function.
If you use var
inside of a block, i.e. a for loop, the
variable is still available outside of that block.
var
has a function scope, not a
block scope.
let x = 5.6;
let
is the block scoped version of
var
, and is limited to the block (or expression) where it is defined.
If you use let
inside of a block, i.e. a for loop, the
variable is only available inside of that loop.
let
has a block scope.
const x = 5.6;
const
is a variable that once it has been
created, its value can never change.
const
has a block scope.
The keyword const
is a bit misleading.
It does not define a constant value. It defines a constant reference to a value.
Because of this you can NOT:
But you CAN: