Some people ask a lot, is there a diffrence between the var keyword and the let keyword? Well, yes, there is.
Explaination
We use let and var to declare variables, to use them with our program.
For example:
let age = 10;
// or
var age = 10;
console.log('Your age is', age);
But then you'll tell me: "If we declare variables with both, why there is a diffrence?". Ok, have an example:
function greet() {
if (true) {
let s = "ahah";
}
console.log(s);
}
greet() // s is not defined, RefrenceError
Why s is undefined? Well because we use let to declare variables in brackets or we can just call them local variables. You can't use it outside the brackets that it is declared in.
But with var:
function greet() {
if (true) {
var s = "ahaa"
}
console.log(s);
}
greet() // ahaa
With var, you declare global variables which you can use it anywhere (of course not outside a function or a class).
Closing
I hope you enjoyed this tutorial, have a great day!
Hi
Some people ask a lot, is there a diffrence between the
var
keyword and thelet
keyword? Well, yes, there is.Explaination
We use
let
andvar
to declare variables, to use them with our program.For example:
But then you'll tell me: "If we declare variables with both, why there is a diffrence?".
Ok, have an example:
Why
s
is undefined? Well because we uselet
to declare variables in brackets or we can just call them local variables.You can't use it outside the brackets that it is declared in.
But with var:
With
var
, you declare global variables which you can use it anywhere (of course not outside a function or a class).Closing
I hope you enjoyed this tutorial, have a great day!
@SixBeeps thanks