-1

What happens to a variable declared in Javascript, if no initial value was assigned to it?

Let's suppose that I declare the variable as below

var cancel;

Now, does this variable "cancel" has a value or is it null or what?

Thanks.

sun_dare
  • 1,146
  • 2
  • 13
  • 33
scorpio98
  • 103
  • 1
  • 2
  • 6
  • http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property-in-javascript <- This would be a better duplicate question to link to. – Mark Jul 24 '14 at 13:42

3 Answers3

4

undefined is a property of the global object, i.e. it is a variable in global scope. The initial value of undefined is the primitive value undefined.

In modern browsers (JavaScript 1.8.5 / Firefox 4+), undefined is a non-configurable, non-writable property per the ECMAScript 5 specification. Even when this is not the case, avoid overriding it.

A variable that has not been assigned a value is of type undefined. A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned.

Since undefined is not a reserved word, it can be used as an identifier (variable name) in any scope other than the global scope.


Example

var x;
if (x === undefined) {
    // these statements execute
}
else {
    // these statements do not execute
}



typeof operator

Alternatively, typeof can be used:

var x;
if (typeof x === 'undefined') {
    // these statements execute
}

One reason to use typeof is that it does not throw an error if the variable has not been defined.

// x has not been defined before
if (typeof x === 'undefined') { // evaluates to true without errors
    // these statements execute
}

if(x === undefined){ // throws a ReferenceError

}



void operator

The void operator is a third alternative.

var x;
if (x === void 0) {
    // these statements execute
}

// y has not been defined before
if (y === void 0) {
    // throws a ReferenceError (in contrast to `typeof`)
}



Source: MDN - Mozilla Developer Network

Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
0

If you just execute the following you will see the alert box with undefined value. Just try yourself in Firebug or chrome developer tool

var cancel;
alert(cancel)
sun_dare
  • 1,146
  • 2
  • 13
  • 33
0

Undeclared variables or variables without a value have a type of 'undefined'.

It's important to note that unlike some other languages (like PHP, for example) 'undefined' is a Javascript keyword that can be checked against, for example:

if(cancel == undefined) { alert('Cancel is undefined!'); };

You can check for the type of a Javascript variable using the javascript

typeof

command, for example:

var cancel;
var typeOfCancel = typeof cancel; // In this case, undefined

var cancel = true;
var typeOfCancel = typeof cancel; // In this case, boolean

This can lead to an extra check, when working with javascript, to see if a variable has been set, as in:

if(cancel == undefined || cancel == null || cancel == '') {
    // React to the empty value here
}

You can see this working here: http://jsfiddle.net/7bu84/

Mark
  • 861
  • 9
  • 17
  • Any explanation for the downvote? – Mark Jul 21 '14 at 16:39
  • Reason is incorrect usage of `==` operator, which coerces `undefined`, `null` and empty string all to falsy, thus invalidating your checks for `undefined`. See also https://stackoverflow.com/q/10560362/1594449. – gknicker Aug 22 '18 at 11:35