How to set a javascript var as undefined

后端 未结 6 690
一整个雨季
一整个雨季 2020-12-14 06:48

Given:

console.log(boo); this outputs undefined

Given:

var boo = 1;
console.log(boo); this outputs 1

Aft

6条回答
  •  半阙折子戏
    2020-12-14 07:14

    Use the void operator. It will evaluate it's expression and then return undefined. It's idiomatic to use void 0 to assign a variable to undefined

    var boo = 1; // boo is 1
    boo = void 0; // boo is now undefined
    

    Learn more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void

提交回复
热议问题