JS Built-ins
← String140 / 195
StringString.prototype.substringbeginner

String.prototype.substring() β€” extract first N chars

πŸ“– Usage Example
const str = 'Hello, World!'
str.substring(7, 12)   // β†’ 'World'
str.substring(5, 2)    // β†’ 'llo' (args swapped)
str.substring(7)       // β†’ 'World!'

Use substring() to extract characters between two indices, automatically swapping them if start is greater than end.

String.prototype.substring() β€” First N Chars

str.substring(0, n) extracts the first n characters.

Challenge: Implement firstN(str, n) that returns the first n characters.

firstN('hello', 3) // β†’ 'hel'
firstN('abcdef', 4) // β†’ 'abcd'
πŸ’‘ Hints (1)
  • β€’ `str.substring(0, n)` is a common pattern for truncating strings.