Object Conversion using toString and valueOf

JavaScript has the three primitives types

  • Boolean
  • String
  • Number

So we can convert the our object to the in one of the three primitive type.

String Conversion (toString() Method)

In JavaScript every object has the toString() method. When we directly display the any method like using the alert or console.log than the toString() method will be called.

var object = {
    FirstName: "James",
    LastName: "Bond"
};

alert(object); // [object Object]

In above example, I directly tried to display the object in the alert. So JavaScript called the toString() method of the object and the toString() method returns the [object Object].

So, let’s override the toString() method of the object.

var object = {
    FirstName: "James",
    LastName: "Bond",

    toString: function () {
        return "Hello, " + this.FirstName + " " + this.LastName + "!!!";
    }
};

alert(object); // Hello, James Bond!!!

So, When you run the above code you will get the now custom message which return by the toString() method.

So we can say that the if the object is directly used to display than the JavaScript internally calls the toString() method.

Array also have the toString() method. So whenever we alert the array than the JavaScript will call the toString() method of the array.

var array = [10, 20, 30];

alert(array); // 10, 20, 30

Now let’s override the toString() method of the array and convert the ,(comma) to the | (vertical pipe)

Array.prototype.toString = function () {
    var str = "";
    for (var i = 0; i < this.length; i++) {
        str += this[i] + " | ";
    }
    return str.substr(0, str.length - 2);
}

var array = [10, 20, 30];

alert(array); // 10 | 20 | 30

Number Conversion (valueOf() Method)

valueOf() method is used by the function which need to return the number like the Math class’s method.

valueOf() method is also called when we trying to convert the string into the number using the +(plus) sign.

var str = "5";

var num = +str;

alert(typeof str); // string
alert(typeof num); // Number

We can also use the Number(obj) function to convert the object to the number.

Number(obj) function is always call the valueOf() method of the object.

var object = {
    Num1: 10,
    Num2: 10            
};

alert(Number(object)); // NaN

As you seen in above example there is no valueOf() method so the Number(obj) function is returned the NaN.

Once we implement that than Number(obj) will return the content of the valueOf() method.

var object = {
    Num1: 10,
    Num2: 10,

    valueOf: function () {
        return this.Num1 + this.Num2;
    }
};

alert(Number(object)); // 20

So it prove that the Number(obj) function will call the valueOf() method of the object.

Boolean Conversion

JavaScript also provides the one more conversion called toBoolean.

Boolean conversion is called when we need to use the object in the if(obj) or while(obj) or do(obj) etc.

There is not such method implemented by the object. JavaScript follows the some hard-coded table of conversion to true and false.

var object = {
    Num1: 10,
    Num2: 10            
};

if (object) {
    alert("Hello world!!!");
}
Value Will Convert to
true/false No need to convert, already in original state.
null, NaN, undefined false
Number 0 (Zero) and NaN will converted to false and others number to the true
String Empty String(“”) converted to the false and other string converted to the true.
Object true (always)