Function scope Vs Block scope

what is the scope of variable

ES5 has function scope & that is because of “hoisting”
ES6 doesn’t have hoisting ,it has block scope

if you declare variable with “var” keyword then “hoisting” will be there
for e p

console.log(x)//undefined
var x = 1;

js compiler will hoist the variable declaration,so the output is undefined,and in fact this section of code will just like this

var x;
console.log(x);
x = 1;

it’s called variable hoisting

block scope is introduced from ES6 onwards
for e,g

 let x = 9
 {
 	let x = 8;
 	console.log(x);//8
}
console.log(x);//9

javascript compiler doesn’t hoist variables declared with “let” keyword

for e p

console.log(x)
let x = 1;

it will throw an errror that is cannot access x before initalization

1. what is hoisting in javasrcipt

A process which is happening behind the scene,internally it is bring the declarations on top

2.How does block scope work

It’s nothing but the brackets {},an if condition,for loop,do while loop etc,
that is a block created which creates separate scope for declarations existing in that block

3.What is scope of a variable

Its’s the availability or visibility or lifetime of a variable

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值