Thursday, January 07, 2016

NTS: JavaScript testing of null, undefined...

In JavaScript, there are a couple of ways to verify if a variable is not undefined.

Undefined

Undefined type is a built-in JavaScript type. undefined (immutable) value is a primitive and is the sole value of the Undefined type. Any property that has not been assigned a value, assumes the undefined value. A function without a return statement, or a function with an empty return statement returns undefined. The value of an unsupplied function argument is undefined. The undefined  global variable which is a reference to the value undefined. In ES3 this variable is mutable, while in ES5 it is immutable.

So the global undefined property represents the primitive value undefined.

Objects are aggregations of properties. A property can reference an object or a primitive. Primitives are values, they have no properties. In JavaScript there are 5 primitive types: undefined, null, boolean, string and number. Everything else is an object. The primitive types boolean, string and number can be wrapped by their object counterparts. These objects are instances of the Boolean, String and Number constructors respectively.

The latest ECMAScript standard defines seven data types (source Mozilla Contributors):
undefined is a a global variable or a property of the global object.

typeof undefined;  // "undefined"

var name = "Paolo Ciccarese";
name = undefined;
typeof name;       // "undefined"

From ES5 the property cannot be re-assigned, and an attempt will fail silently. By turning on the 'strict mode' attempting to assign a value to undefined will throw an error instead. In general, even when possible, re-assigning undefined is not a good idea.

The Global object is an intrinsic object whose purpose is to collect global functions and constants into one object. The global object itself can be accessed using the this operator in the global scope. The Global object cannot be created using the new operator. It is created when the scripting engine is initialized, thus making its functions and constants available immediately.

Undefined: Local or Declared Variables


if (name === undefined) ... // Works for local and/or declared variables

This approach (JSFiddle) works when the variable has been defined for instance in a function:
function myFunction(foo) {
    if (foo === undefined) {
        alert('sorry, that is undefined');
    }
}
myFunction();

However, if the variable has never been declared, that would trigger an exception (JSFiddle - see the console):
function myFunction() {
    if (foo === undefined) { // Triggers a ReferenceError exception
        alert('sorry, that is undefined');
    }
}
myFunction();

Therefore, this approach works well for local variables that we know have been declared. It is not a convenient approach for local variables that might have not been declared.

Avoid the following as it returns true also for null:
if (typeof foo == 'undefined') ... // Returns true also for null

Undefined: Global Variables


if(typeof name === "undefined") ... // Works for global and local variables

This approach works for both declared (JSFiddle):
function myFunction(foo) {
    if (typeof foo === 'undefined') {
        alert('sorry, that is undefined');
    }
}
myFunction();

and undeclared variables (JSFiddle):
function myFunction() {
    if (typeof foo === 'undefined') {
        alert('sorry, that is undefined');
    }
}
myFunction();

We might argue this last approach is safer in general.

Null

The Null (type) has exactly one value, called null (value). The value null is a JavaScript literal representing null or an "empty" value, i.e. no object value is present. It is one of JavaScript's primitive values and also - unlike undefined - a JavaScript keyword. Because of being a keyword, null is inherently unassignable, while ES5 had to add a special rule to cover the undefined global variable.

null is classified as a JavaScript Object:
typeof(null) // object
typeof(undefined) // undefined
The value null is a JavaScript literal representing null or an "empty" value, i.e. no object value is present. In other words we can assign null to a variable without a value:
var nv = null; // null
typeof nv      // object

Notice that:
null === undefined // false
null == undefined  // true*
null == false;     // false
null == '';        // false
null == 0;         // false
* see language specifications here


Testing for Null


So if we test with:
if (x == null) ... // Tests both null and undefined
We are actually testing for name to be null and undefined at the same time. But if we test with:
if (x === null) ... // Tests for null only

Testing for Undefined and Null

For testing both null and undefined (when a variable is declared) it is possible to do:
if (x != null)  ... // Tests both null and undefined

Ore more explicitly:
if (x !== undefined && x !== null)  ... // Tests both null and undefined

And to prevent ReferenceError for undeclared variables:
if (typeof x !== 'undefined' && x !== null)  ... // Tests null, undefined

Falsey values

In JavaScript we can also test with the following:
if (!x) ... // Tests for falsey values

Which will be evaluated true if name is: null, undefined, NaN, empty string ("" or ''), 0, -0, false. This checking has to be used carefully when the value of the data could be 0 or false or an empty string.

No comments: