This simple function checks to see if something "is not a number". It returns true if the input is not a number.
isNaN(24) => False // 24 is a number
isNaN("cat") => True // "cat" is a string
isNaN("24") => False // "24", even though a string, is converted to a number by Javascript
isNaN(true) => False // true = 1
isNaN(NaN) => True // Not a Number is Not a Number ;)
isNaN(undefined) => True*
But...
var cat = 10;
isNaN(cat) => False // the variable cat has a value of 10
Undefined
Codecademy tossed this "undefined" variable into the lesson on isNaN. I'd like to know what it means. Off to the experts we go.
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 1undefined`.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 returnsundefined
if a value was not returned.
In short, undefined
is a permanent variable that is always available. It will be returned if another variable, which has not been defined is called. Also, it can be used cleverly in if else statements, like this:
// if I haven't defined "myVar" do this
if (myVar = undefined) {
do something;
}
// if I have defined "myVar" do this
else {
do something else;
}