JS Built-ins
← Array15 / 226
ArrayArray.fromintermediate

Array.from() β€” count Unicode code points

πŸ“– Usage Example
Array.from('hello')
// β†’ ['h', 'e', 'l', 'l', 'o']

Use Array.from() on a string to split it into an array of individual characters.

Array.from() β€” Unicode-safe string splitting

str.split('') splits a string by UTF-16 code units, which breaks emoji and other characters outside the Basic Multilingual Plane into two surrogates. Array.from(str) iterates by code point, keeping emoji intact.

Challenge: Implement countCodePoints that returns the number of Unicode code points in a string using Array.from(str).length.

countCodePoints('hello')   // β†’ 5
countCodePoints('hi πŸ‘‹')   // β†’ 4  (not 5 β€” emoji is one code point)
πŸ’‘ Hints (2)
  • β€’ `Array.from(str)` uses the string's Symbol.iterator which yields code points.
  • β€’ Compare `Array.from("πŸ‘‹").length` (1) vs `"πŸ‘‹".split("").length` (2) to see the difference.