Well apart from that, another reason that makes me obliged to write about this is that javascript is highly flexible about the type of values it requires. Trust me this will sometime make you confused that how sometime one type converts to another!!!
Some of this behavior I have already described in first part of this series - When javascript expects a boolean value, you may supply a value of any type and javascript will convert it as needed.
This type conversion flexibility is supported for other types as well. What I mean to say is that when javascript expects a number or a string, it will automatically convert to the value of that type.
In this blog I will discuss about the automatic conversion of different types to number type by javascript.
The very first thing, before we start anything on this topic I will ask you to have a look at the below code snippets (If possible i would request you to try these out in the browsers developers tool as well)
null * 2; // 0
false * 2 // 0
true * 2 // 2
"4" * "2" // 8
undefined * 2 //NaN
"a" * 2; // NaN
//check below isNaN check also
isNaN(""); //false
isNaN(true); //false
isNaN(false) //false
isNaN("4") //false
isNaN("a") //true
So the basic concept is, when required:
2.) true will convert to numeric 1.
3.) empty string "" will convert to numeric 0.
4.) non- empty string having numeric values will convert to corresponding numeric value of type number.
5.) non- empty string having non-numeric values will convert to NaN.
6.) null converts to 0.
7.) undefined converts to NaN.
Now I will be talking about the thing which actually will bring bewildered expressions on your faces. Try the below examples now....
[9] * 2; // 18
[9,2] * 2; // NaN
["a"] * 2; //NaN
isNaN([]); // false
isNaN([9]); // false
isNaN([9,2]); // true
isNaN(new Date()); //false
But the question is will javascript convert all objects to number types when required?
To get the answer of this question, we need to know the logic that javascript puts in converting the object type to number type.
Javascript follows the following steps to convert any object type to number type:
i.) If the object has valueOf() method, this method will be called. This method returns the primitive value. Now if this primitive value is not a number then javascript will use the primitive to primitive auto type conversion to change it to number type.
ii.) Otherwise, javascript will check to see if the object has a toString() method. If yes, then this method will be called, which again returns the primitive value. Now the javascript will use primitive to primitive conversion rules to change it to number.
iii.) Otherwise, finally you will get a TypeError....
To elaborate this point I will show you one more example:
{
this.value = arg;
}
//overide the valueOf method
TestClass.prototype.valueOf = function()
{
return this.value;
}
var obj1 = new TestClass(9);
var obj2 = new TestClass("a");
console.log(obj1 * 2) // prints 18
isNaN(obj1) // returns false
console.log(obj2 * 2) // prints NaN
isNaN(obj2) // returns true
You may try the above example by overriding the toString method as well.
And yes check out what valueOf method for Date object returns, to understand why it got successfully converted to number type :)