JS Built-ins
← String188 / 195
StringString.prototype.valueOfintermediate

String.prototype.valueOf() β€” String object primitive value

πŸ“– Usage Example
const str = 'Hello'
str.valueOf()               // β†’ 'Hello'
new String('hi').valueOf()   // β†’ 'hi'

Use valueOf() to return the primitive string value, useful when working with String wrapper objects.

String.prototype.valueOf() β€” String Object

A String object wraps a primitive. valueOf() unwraps it to a primitive.

Challenge: Implement objectValueOf(strObj) that extracts the primitive from a String object.

objectValueOf(new String('hello')) // β†’ 'hello'
πŸ’‘ Hints (1)
  • β€’ `new String("hello").valueOf()` returns the primitive `"hello"`.