Object to Primitive Conversion
Objects the Basics: Object to Primitive Conversion
What is the rule for objects in a Boolean context?
View Answer:
Interview Response: All objects are true in a Boolean context.
What are the three variants of type conversion in JavaScript?
View Answer:
Interview Response: String, number, and default conversions.
Technical Response: The three variants of type conversion include string, number, and default conversions. String conversion can happen explicitly when an object expects a string, and mathematical operations use explicit number conversion on primitives. In rare circumstances where the operator is unclear about what type to anticipate, the default gets used.
To implement conversions, what are the three object methods JavaScript tries to find and call?
View Answer:
Interview Response: The three object methods include Symbol.toPrimtive (system symbol) if it exists. Otherwise, if the hint is a string, it will try Obj.toString() or Obj.valueOf(). Finally, if the hint is a number or default it will try Obj.valueOf() and Obj.toString().
Simplified: The three object methods include Symbol.toPrimitive, Obj.toString(), and/or Obj.valueOf().
Explain what Symbol.toPrimitive() is and what it does?
View Answer:
Interview Response: The Symbol.toPrimitive is a symbol that specifies a function valued property called to convert an object to a corresponding primitive value.
Code Example:
let user = {
name: 'John',
money: 1000,
[Symbol.toPrimitive](hint) {
alert(`hint: ${hint}`);
return hint == 'string' ? `{name: "${this.name}"}` : this.money;
},
};
// conversions demo:
alert(user); // hint: string -> {name: "John"}
alert(+user); // hint: number -> 1000
alert(user + 500); // hint: default -> 1500
What is the outcome when you try to use a for…loop to expose the properties of an object using Symbol.toPrimitive()?
View Answer:
Interview Response: The result returns all properties except for the Symbol.toPrimitive because JavaScript does not expose Symbols in the global symbol registry in this fashion.
Code Example:
let user = {
name: 'John',
money: 1000,
[Symbol.toPrimitive](hint) {
alert(`hint: ${hint}`);
return hint == 'string' ? `{name: "${this.name}"}` : this.money;
},
};
for (let prop in user) {
console.log(prop); // returns name, money but no Symbol
}
Methods toString and valueOf come from ancient times. Are these two methods considered Symbols?
View Answer:
Interview Response: No, because toString and valueOf came before Symbols debuted in the JavaScript codebase. They are regular string-name methods.
Technical Response:No, because toString and valueOf came before Symbols debuted in the JavaScript codebase. They are regular string-name methods. If there is no Symbol.toPrimitive, JavaScript tries to find them.
By default, a plain object has the following toString and valueOf methods. What does each of these object methods return?
View Answer:
Interview Response: The toString method returns a string "[object Object]", and the valueOf method returns the object itself.
Code Example:
let user = { name: 'John' };
alert(user); // [object Object]
alert(user.valueOf() === user); // true
What happens if toString or valueOf returns an object?
View Answer:
Interview Response: There is no error, but such value gets ignored.
Technical Response: For historical reasons, if toString or valueOf returns an object, there is no error, but such value is ignored (like if the method did not exist). That is because, in ancient times, there was no good "error" concept in JavaScript.
As you pass an object as an argument, what are the stages?
View Answer:
Interview Response: The object gets converted to a primitive. If the resulting primitive is not the right type, it gets converted.
Technical Response: If we pass an object as an argument, there are two stages.
- The object gets converted into a primitive.
- If the resulting primitive is not of the right type, it gets converted.
Code Example:
let obj = {
// toString handles all conversions in the absence of other methods
toString() {
return '2';
},
};
alert(obj * 2); // 4, object converted to primitive "2", then multiplication made it a number