Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Boolean.valueOf in JavaScript

Typeof of Boolean.valueOf is a function but what does it do? Why object versions of primitives have these methods like Array.valueOf? It's not the same as for example Boolean.prototype.valueOf...

Also what's the point of Boolean.toString() it just returns this: "function Boolean() { [native code] }". Also other objects like Array, Number have it...

like image 900
Michał Kownacki Avatar asked Dec 20 '22 10:12

Michał Kownacki


2 Answers

Why it happens

Boolean is a function, just like String and Object, you can call it as a function to convert something to a boolean for example:

Boolean(3); // true

All functions in JavaScript are objects, and as objects they have a .valueOf property. Since Boolean is a function, its valueOf isn't very meaningful though. Since it's calling Object#valueOf via prototypical inheritance it just returns the same object

alert(Boolean.valueOf() === Boolean) // true

// for the same reason
var x = {};
alert(x.valueOf() === x); // true

The specification states:

15.2.4.4 Object.prototype.valueOf

Let O be the result of calling ToObject passing the this value as the argument.

Which in turn does:

9.9 ToObject

The result is the input argument (no conversion).

What it's for

The reason .valueOf exists is to provide an interface for explicit value obtaining. It is used extensively in the background of the language (as the other answer mentions, much like .toString). For example it can take a Number object and convert it into the primitive number data type.

Here is an example:

3 == {valueOf: function(){ return 3; }} // true

4 < {valueOf: function(){ return 2; }} // false

Math.max(3, {valueOf: function(){ return 5; }}) // 5, not the object

It's somewhat incomplete and in all honesty, we don't have a good solution in JavaScript for user defined values until ES7 kicks in with value types.

like image 109
Benjamin Gruenbaum Avatar answered Dec 31 '22 10:12

Benjamin Gruenbaum


It seems that .valueOf is similar to .toString method according to output.

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.

By default, the valueOf method is inherited by every object descended from Object. Every built-in core object overrides this method to return an appropriate value.

Source

Basically .valueOf is used to return primitive version of object, but you don't need to call it yourself. You could use it in your advantage in your library. For example, you have a request object instead of writing your own method, you could extend .valueOf and return status of request.

Also what's the point of Boolean.toString() it just returns this: "function Boolean() { [native code] }".

I don't sure about Boolean.toString(), but Boolean.prototype.toString() is quite helpful. With .toString method you can get string representation of any object, consider this example:

var bool = true;

console.log(bool.toString()); // "true"

In some cases it's could be helpful to get string representation of an object, but in most cases JS automatically convert object in string during string concatenation (+ operator).

More about .toString.

like image 26
volter9 Avatar answered Dec 31 '22 11:12

volter9