40

Given:

console.log(boo); this outputs undefined

Given:

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

After defining boo and setting to 1, how can I then reset boo, so that console.log outputs undefined?

Thanks

Bryan Kyle
  • 13,361
  • 4
  • 40
  • 45
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

6 Answers6

50

Solution

To reliably set a variable boo to undefined, use a function with an empty return expression:

boo = (function () { return; })();

After executing this line of code, typeof(boo) evaluates to 'undefined', regardless of whether or not the undefined global property has been set to another value. For example:

undefined = 'hello';
var boo = 1;
console.log(boo); // outputs '1'
boo = (function () { return; })();
console.log(boo); // outputs 'undefined'
console.log(undefined); // outputs 'hello'

EDIT But see also @Colin's simpler solution!

Reference

This behavior is standard as far back as ECMAScript 1. The relevant specification states in part:

Syntax

return [no LineTerminator here] Expression ;

Semantics

A return statement causes a function to cease execution and return a value to the caller. If Expression is omitted, the return value is undefined.

To view the original specifications, refer to:

Appendix

For completeness, I have appended a brief summary of alternate approaches to this problem, along with objections to these approaches, based on the answers and comments given by other responders.

1. Assign undefined to boo

boo = undefined; // not recommended

Although it is simpler to assign undefined to boo directly, undefined is not a reserved word and could be replaced by an arbitrary value, such as a number or string.

2. Delete boo

delete boo; // not recommended

Deleting boo removes the definition of boo entirely, rather than assigning it the value undefined, and even then only works if boo is a global property.

Community
  • 1
  • 1
Gooseberry
  • 652
  • 1
  • 5
  • 13
  • 9
    Regarding your appendix item 1: from the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined): "In modern browsers (JavaScript 1.8.5 / Firefox 4+), `undefined` is a non-configurable, non-writable property per the ECMAScript 5 specification." So `boo = undefined;` is safe in "modern browsers" because `undefined` cannot be overridden. – TachyonVortex Nov 07 '14 at 23:32
  • 3
    isn't the return statement redundant? This worked for me: `_foo = (function () {})();` – Andresch Serj Feb 11 '15 at 14:47
  • 3
    @AndreschSerj That worked for me too. The return statement is redundant. My IDE also complained about it :) – Mickey Puri Feb 18 '15 at 11:10
  • 1
    See also Colin's simpler solution [here](http://stackoverflow.com/a/24748543/3768862). – Gooseberry Apr 26 '16 at 21:06
44

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

Colin
  • 852
  • 7
  • 6
16

You can simply assign a variable the value of undefined:

boo = undefined;

Alternatively, you can use the delete operator to delete the variable:

delete boo;
Bryan Kyle
  • 13,361
  • 4
  • 40
  • 45
9

delete boo

Don't use var boo = undefined. undefined is just a variable and if someone sets undefined = "hello" then you'll be getting hello everywhere :)

EDIT:

null wasn't same as undefined. removed that bit.

neebz
  • 11,465
  • 7
  • 47
  • 64
  • 8
    You cannot `delete boo`, it's a variable. It only works if the variable is in global scope and even then it doesn't work in IE and throws an error in strict mode. – Esailija Jul 26 '12 at 14:07
  • Please be aware that this may not work in every JavaScript runtime environment. I tried this in Node.js and it did not work. – Lee Jenkins Apr 16 '14 at 17:57
  • 3
    Esailija is correct. From the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete): "`delete` is only effective on an object's properties. It has no effect on variable or function names." – TachyonVortex Nov 07 '14 at 23:08
3
var boo = 1;
console.log(boo); // prints 1
boo = undefined;
console.log(boo); // now undefined
McStretch
  • 20,495
  • 2
  • 35
  • 40
2

This works on Chrome Javascript Console:

delete(boo)
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
lobster1234
  • 7,679
  • 26
  • 30