AnApprentice
AnApprentice

Reputation: 110960

How to set a javascript var as undefined

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

Upvotes: 40

Views: 46929

Answers (6)

Gooseberry
Gooseberry

Reputation: 652

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.

Upvotes: 50

Colin
Colin

Reputation: 882

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

Upvotes: 45

lobster1234
lobster1234

Reputation: 7779

This works on Chrome Javascript Console:

delete(boo)

Upvotes: 2

neeebzz
neeebzz

Reputation: 11538

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.

Upvotes: 9

Bryan Kyle
Bryan Kyle

Reputation: 13761

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;

Upvotes: 16

McStretch
McStretch

Reputation: 20645

var boo = 1;
console.log(boo); // prints 1
boo = undefined;
console.log(boo); // now undefined

Upvotes: 3

Related Questions