The significance of void 0 in JS

What

void: prefix operator
void 0 will return undefined as well

Why

Why using void 0 alternative to undefined?
Because undefined is a global variable:
In early, you can change the undefined directlty

console.log(undefined) // Output: "undefined"
var undefined = "I am new value"
console.log(undefined) // Output: "I am new value"

Now, the undefined is set read-only, but you still change it in local scope

{
	const undefined = "I am new value"
	console.log(undefined) // Output: "I am new value"
}

It will be significant that avoiding certain exception by using void 0.
Meanwhile, another trivial advantage is void will occupies less byte compared with undefined.

Summary

  1. Avoid exception sinceundefined is a global variable can be changed arbitrarily under some conditions.
  2. occupy less byte

Reference

https://stackoverflow.com/questions/7452341/what-does-void-0-mean

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值