Horje
Unleashing the Power of valueOf in JavaScript

In JavaScript, The valueOf() method is a fundamental method that allows objects to specify how they should be converted to primitive values. When an object is used in a context where a primitive value is expected JavaScript automatically calls the valueOf() method on the object to get its primitive representation.

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Customizing valueOf() for Basic Object

Customizing the valueOf() method for basic JavaScript objects allows you to define how the object should be represented as a primitive value. By default, when JavaScript tries to convert an object to a primitive it will call the valueOf() method on the object if it exists.

Syntax:

const geek = {
    value: 51,
    valueOf() {
        return this.value;
    }
};

Example: In this example, we are using the above-explained approach.

Javascript

const geek = {
    value: 51,
    valueOf() {
        return this.value;
    }
};
console.log(geek + 5);

Output

56

Approach 2: Customizing valueOf() for Custom Class

You can create a custom class and customize the valueOf() method to define how instances of the class should behave when coerced to a primitive value. By default, when an object is used in a context where a primitive value is expected and JavaScript will call the valueOf() method to obtain the primitive representation of the object.

Syntax:

class geek {
    constructor(value) {
        //code
    }
    valueOf() {
        // code
    }
};

Example: In this example, we are using the above-explained approach.

Javascript

class geek {
    constructor(value) {
        this.value = value;
    }
    valueOf() {
        return this.value;
    }
}
const obj = new geek(110);
console.log(obj + 30);

Output

140

Approach 3: Exploiting valueOf() in Math Operations

You can leverage the valueOf() method to customize how an object is treated in mathematical operations. you can make objects behave like primitive values in arithmetic expressions and This can be useful when working with custom objects and you want them to interact seamlessly with the JavaScript’s built-in arithmetic operations.

Syntax:

const gfg = {
    hours: 4,
    minutes: 20,
    valueOf() {
        return this.hours * 50 + this.minutes;
    }
};

Example: In this example, we are using the above-explained approach.

Javascript

const gfg = {
    hours: 4,
    minutes: 20,
    valueOf() {
        return this.hours * 50 + this.minutes;
    }
};
const totalTimeInMinutes = gfg + 13;
console.log(totalTimeInMinutes);

Output

233



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Host HTML, CSS & JavaScript Website on Vercel ? How to Host HTML, CSS & JavaScript Website on Vercel ?
Bit manipulation in JavaScript Bit manipulation in JavaScript
Interactive Data Visualizations in JavaScript Interactive Data Visualizations in JavaScript
JavaScript Program to Merge Two Arrays Without Creating a New Array JavaScript Program to Merge Two Arrays Without Creating a New Array
JavaScript Program for K-th Largest Sum Contiguous Subarray JavaScript Program for K-th Largest Sum Contiguous Subarray

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13