JS Built-ins
← Object136 / 150
ObjecttoLocaleStringbeginner

toLocaleString() β€” plain object default

πŸ“– Usage Example
// Get locale-aware string representation
const date = new Date(2024, 0, 1)
date.toLocaleString()   // β†’ locale-specific date string
const num = 1234567
num.toLocaleString()    // β†’ '1,234,567'

Use toLocaleString() to get a locale-sensitive string representation of a value, most useful on Date and Number objects.

Object.prototype.toLocaleString()

obj.toLocaleString() calls toString() by default for plain objects, returning "[object Object]".

It is designed to be overridden by subclasses (like Date, Number, Array) that have locale-specific string representations.

Challenge: Implement getLocaleString(obj) that returns the locale string of the object.

πŸ’‘ Hints (1)
  • β€’ Plain objects inherit toLocaleString from Object.prototype, which calls toString()