DEV Community

kavin K
kavin K

Posted on

Java script c-2 variables

i)let
ii)const
iii)var

Let,var -almost same
let-It works in {} only
var-It works in globally.outoff {}

const-we cannot change the value.
Eg.amazon order limit.

Program:

clickme
</button)

addnumbers - function name
clickme - button name

Program:
let a=100
function addnumbers()
console.log(a)

Op:
100

var name="kavin"
var name="Praveen"
var name="naresh"
console.log(name)

Op:
naresh
console.log(name) access immediate reference.

*we can't initiate one or more variable with same name in let and const.But we do that in 'var'.

Demerits of var:

i)initialzes more reference values in same name. It has more chance to create bug.

function addNumbers(){
var a=10;
{
var a=15;
}
console.log(a);
}
console.log(a);----->
Op:
a is not defined
Var-It is afunction scoping. It works in within function only {}

If stop the the last "console.log(a);" we will get the op of value is "15".

let vs var:
we can access the value out of scoping{} in var.

Eg:
{
var a=15;
}
console.log(a);
Op:
15

Hosting:
function addnumbers(){
console.log(a);
var a=10;
{
var a=15;
}
console.log(a);
}
Op:
undefined
15

if we create var, it initially move to the top and it assigned to undefined.

Top comments (0)