Code Logo

Create Array with Array.from()

Published at25 Jul 2026
JavaScript Collections Easy 0 views
Like0

Write a JavaScript function that takes a string and returns an array of its individual characters using Array.from(). The Array.from() static method creates a new shallow-copied array from an iterable or array-like object.

Array.from() is a static method that converts iterable or array-like objects into arrays. Unlike str.split('') which also splits a string into characters, Array.from() correctly handles Unicode characters that consist of two UTF-16 code units (like emoji). It also accepts an optional map function for transforming elements during creation.

Array.from() can create arrays from: strings, Set, Map, NodeList, TypedArrays, and any iterable with Symbol.iterator. It is more robust than spread operator for array conversion because it works with array-like objects that don't have Symbol.iterator.

Time complexity is O(n) where n is the string length. Space complexity is O(n) for the resulting array. Array.from() creates a new array without modifying the source string.

Edge cases include empty strings (returns empty array), single-character strings, strings with special Unicode characters, and ensuring each character becomes a separate array element.

Example Input & Output

Example 1
Input
"hello"
Output
["h","e","l","l","o"]
Explanation

Split into characters

Example 2
Input
"x"
Output
["x"]
Explanation

Single character

Example 3
Input
"123"
Output
["1","2","3"]
Explanation

Digits as characters

Example 4
Input
"abc"
Output
["a","b","c"]
Explanation

Simple string

Example 5
Input
""
Output
[]
Explanation

Empty string

Algorithm Flow

Recommendation Algorithm Flow for Create Array with Array.from()
Recommendation Algorithm Flow for Create Array with Array.from()

Solution Approach

function solution(str) {
  return Array.from(str);
}

Best Answers

javascript - Approach 1
function solution(str) {
  return Array.from(str);
}