JS Built-ins
← String159 / 195
StringString.prototype.toStringbeginner

String.prototype.toString() β€” in template literal

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

Use toString() to return the primitive string value of a string or String object.

String.prototype.toString() β€” Template Literal

When you embed a value in a template literal, JavaScript calls .toString() implicitly.

Challenge: Implement wrapInGreeting(name) that returns 'Hello, <name>!'.

wrapInGreeting('World') // β†’ 'Hello, World!'
πŸ’‘ Hints (1)
  • β€’ Template literals automatically call `.toString()` on embedded values.