Word to Array
Ande Caleb

Ande Caleb @andaeiii

About: Senior FrontEnd Developer ( React / Next / TS / Tailwind / WebGL(ThreeJS + GSAP) open to REMOTE work...

Location:
Abuja, Nigeria
Joined:
Jul 19, 2022

Word to Array

Publish Date: Jul 26 '22
3 2

a simple way to convert a String to an Array is by Spreading it... within the array brackets [ ... ] symbols, which is easier than using the String.prototype.split()

let str = 'Aminu Kano';

array = [ ...str ]; //break the array into letters. 



Enter fullscreen mode Exit fullscreen mode

This outputs an array of 10 elements. which becomes

[ "A", "m", "i", "n", "u", " ", "K", "a", "n", "o" ];

//same result can be achieved using the code below. 

str.split("");

Enter fullscreen mode Exit fullscreen mode

but the latter seems much easier and cleaner

hope this helps..

Comments 2 total

Add comment