Split a String into a List of Characters in JavaScript and Node.js

JavaScript allows you to split strings into an array of string values at individual characters. A hidden functionality is splitting a string into a list of characters.

And that’s what this tutorial shows you: how to split a given string value into a list of its individual characters. It also keeps emojis „as is” without splitting the emojis into their composition characters.

Node.js Series Overview

Split a String into a List of Characters

JavaScript’s string values are iterable. For example, you may use a for-loop to iterate through the individual characters of a string.

You can use the string iterator to split a string into a list of characters. Pass a given string value into the Array.from() method and you’re receiving an array of characters in return:

/**
 * Determine whether the given `input` is a string.
 *
 * @param {*} input
 *
 * @returns {Boolean}
 */
function chars (string) {  
  return Array.from(
    String(string)
  )
}

chars('Hello Marcus')  
// ['H', 'e', 'l', 'l', 'o', ' ', 'M', 'a', 'r', 'c', 'u', 's', ]

chars('')  
// []

Use the @supercharge/strings Package

I’m the maintainer of the @supercharge/strings package providing convenient string utilities. It provides a useful Str#chars(string) method returning the list of individual characters of the given string value:

const Str = require('@supercharge/strings')

Str('Hello Marcus').chars()  
// ['H', 'e', 'l', 'l', 'o', ' ', 'M', 'a', 'r', 'c', 'u', 's', ]

Str().chars()  
// []

Enjoy!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.