The Wayback Machine - https://web.archive.org/web/20240118164900/https://www.geeksforgeeks.org/how-to-get-character-array-from-string-in-javascript/
Open In App
Related Articles

How to Get Character Array from String in JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The string is defined as a sequence of characters. In this article, we will learn how to get a character array from a string in JavaScript.

Method 1: JavaScript String split() Function

The str.split() function is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. 

Syntax:

str.split(separator, limit);

 

Example: In this example, we will get a character array from the string using the JavaScript split() method.

Javascript




// Input string
let str = "GeeksforGeeks";
  
// Use string.split() to Get Character Array
let arr = str.split("");
               
// Display output
console.log(arr);


Output

[
  'G', 'e', 'e', 'k',
  's', 'f', 'o', 'r',
  'G', 'e', 'e', 'k',
  's'
]

Method 2: JavaScript Array.from() Function

The Array.from() function creates a new array instance from a given array. In case of string, every alphabet of the string is converted to an element of the new array instance, and in the case of integer values, a new array instance simply takes the elements of the given array. 

Syntax:

Array.from(str);

Example: In this example, we will get a character array from the string using the Array.from() function in JavaScript.

Javascript




// Input array
let str = "GeeksforGeeks: A computer science portal";
  
// Getting char array
let arr = Array.from(str);
            
// Display output          
console.log(arr);


Output

[
  'G', 'e', 'e', 'k', 's', 'f', 'o',
  'r', 'G', 'e', 'e', 'k', 's', ':',
  ' ', 'A', ' ', 'c', 'o', 'm', 'p',
  'u', 't', 'e', 'r', ' ', 's', 'c',
  'i', 'e', 'n', 'c', 'e', ' ', 'p',
  'o', 'r', '...

Method 3: JavaScript Spread Operator

The Spread operator allows an iterable to expand in place. In the case of a string, it example string into character and we capture all the characters of a string in an array.  

Syntax:

let variableName = [ ...value ];

Example: In this example, we will get a character array from the string using the spread operator in JavaScript.

Javascript




//Input array
let str = "GeeksforGeeks";
  
//Display output using spread operator
console.log([...str]);


Output

[
  'G', 'e', 'e', 'k',
  's', 'f', 'o', 'r',
  'G', 'e', 'e', 'k',
  's'
]

Method 4: Using for … of Loop

JavaScript for of statement iterates over the values of an iterable object (like Array, Map, Set, arguments object, …,etc), executing statements for each value of the object.

Syntax:

for ( variable of iterableObjectName) {
...
}

Example: In this example, we are using for…of… loop.

Javascript




const s = '12345';
const a = [];
for (const s1 of s) {
   a.push(s1);
}
console.log(a);


Output

[ '1', '2', '3', '4', '5' ]

 

You can learn JavaScript from this JavaScript Tutorial and practice on JavaScript Examples.


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 28 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials