March 14, 2001 - Finding the Object's Value with valueOf() | WebReference

March 14, 2001 - Finding the Object's Value with valueOf()

Yehuda Shiran March 14, 2001
Finding the Object's Value with valueOf()
Tips: March 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

You can probe the value of an object by using the Object object's method of valueOf(). When the object consists of a number, like:

var myNum = new Number(35);

the valueOf() method returns the string "35". When the object is user-defined, the method returns [object Object]. Try it for the following Employee object:

function Employee() {
  this.dept = "HR";
  this.manager = "John Johnson";
}
function printProp() {
  var Ken = new Employee();
  alert(Ken.valueOf());
}