JavaScript Object.prototype.valueOf() Method Last Updated : 10 Aug, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In JavaScript, the Object.prototype.valueOf() method is used to return the primitive value of the specified object. Whenever a primitive value is required, JavaScript automatically invokes the valueOf() method. The valueOf() method is automatically inherited by every object in JavaScript. Every object overrides this method to return an appropriate primitive value. If no primitive value is present for this object, then JavaScript returns the object itself. We can override this method to convert any built-in object to a primitive value. In the case of Custom object types, we override this method to call a custom method. If we are dealing with custom object types, we can use the following syntax to override valueOf() method for it : Syntax:ObjectType.prototype.valueOf = function() { return CustomPrimitiveValue; };In this syntax, ObjectType: The custom object type created by the user.CustomPrimitiveValue: The primitive value of the specified object.Although the valueOf() method is automatically invoked in JavaScript, we can use the following syntax to invoke it ourselves Syntax: ObjectType.valueOf()Example: In this example, we tried to override the valueOf() method to return the primitive value as the actual number plus 3. Thus the primitive value returned after passing 18 as the argument was 21. As we tried to log primitive value minus 12, we got 21-12 which is 9 as our final answer. JavaScript function myFunction() { // Creating a custom object // type ExType function ExType(n) { this.number = n; } // A callback method overriding // the valueOf() method ExType.prototype.valueOf = function () { // Returned valued is 18+3 which is 21 return this.number + 3; }; // Creating an object of ExType object type const object1 = new ExType(18); // Logs 21-12 which is 9 console.log(object1 - 12); } myFunction(); Output9Example: In this example, the person object has properties of name and age. toString() returns the name and age, and valueOf() returns the age as the primitive value. JavaScript const person = { name: "John", age: 30, toString() { return `Name: ${this.name}, Age: ${this.age}`; }, valueOf() { return this.age; } }; console.log(person.toString()); // Output: "Name: John, Age: 30" console.log(person.valueOf()); // Output: 30 OutputName: John, Age: 30 30Supported Browsers: Chrome 1 and aboveEdge 12 and aboveFirefox 1 and aboveOpera 3 and aboveSafari 1 and above Comment More infoAdvertise with us Next Article JavaScript Object values() Method A ashutoshrathi Follow Improve Article Tags : JavaScript Web Technologies javascript-object JavaScript-Methods Similar Reads JavaScript Object setPrototypeOf() Method The Object.setPrototypeOf() method in JavaScript is a standard built-in object thatthat will sets the prototype (i.e., the internal [[Prototype]] property) of a specified object to another object or null.Syntax:Object.setPrototypeOf(obj, prototype)Parameters:This method accepts two parameters as men 2 min read JavaScript Object.prototype.toString() Method In JavaScript, the Object.prototype.toString() method is used to return a string that can represent the object. The toString() method is automatically inherited by every object which is inherited from Object. Whenever an object is represented as a text value or a string is expected from the object, 3 min read JavaScript Object values() Method JavaScript object.values() method is used to return an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by the object manually if a loop is applied to the properties. Object.values() takes the object as an argument 3 min read JavaScript Reflect setPrototypeOf() Method JavaScript Reflect.setPrototypeOf() method in JavaScript is used to set the prototype of a specified object to another object or to Null. This method returns the boolean value for the operation that is successful. This method is the same as the Object.setPrototypeOf() method. Syntax: Reflect.setProt 2 min read JavaScript Number valueOf( ) Method JavaScript Number valueOf( ) method in JavaScript is used to return the primitive value of a number. This method is usually called internally by JavaScript and not explicitly in web code Syntax: number.valueOf() Parameters: This method does not accept any parameter. Return Value: The valueof() metho 1 min read JavaScript Object propertyIsEnumerable() Method The propertyIsEnumerable() method returns a Boolean indicating whether the specified property is enumerable and is the object's own property. The propertyIsEnumerable() method returns false if the object doesn't have the specified property. Syntax: obj.propertyIsEnumerable(prop) Parameters: This met 1 min read Like