JS Built-ins
← String194 / 195
StringString.prototype.lengthbeginner

String.prototype.length β€” spaces count toward length

πŸ“– Usage Example
'hello'.length    // β†’ 5
''.length         // β†’ 0
'πŸ˜€'.length       // β†’ 2

The length property returns the number of UTF-16 code units in the string.

String.prototype.length β€” Spaces Count

Spaces are characters too and are counted in .length.

Challenge: Implement lengthWithSpaces(str) that returns the length including spaces.

lengthWithSpaces('hello world') // β†’ 11
πŸ’‘ Hints (1)
  • β€’ Every character β€” including whitespace β€” contributes 1 to `.length`.