I want to know what is exactly the use of valueOf() and when valueOf() is used?
In the course, JavaScript in Detail, the explanation is: " The .valueOf()
will extract the value of the target type from the object." and on MDN the explanation is: " JavaScript calls the valueOf
method to convert an object to a primitive value. You rarely need to invoke the valueOf
method yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected."
I’m still not sure what they mean. Do we use it or not use it as MDN says “You rarely need to invoke the valueOf
method yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected.”
Kindly explain.