How to get the same result as C# String.ToCharArray() with JavaScript?

Sometimes, we want to get the same result as C# String.ToCharArray() with JavaScript.

In this article, we’ll look at how to get the same result as C# String.ToCharArray() with JavaScript.

How to get the same result as C# String.ToCharArray() with JavaScript?

To get the same result as C# String.ToCharArray() with JavaScript, we can use the spread operator.

For instance, we write:

const str = "12345";
const strArr = [...str];
console.log(strArr)

to spread the str string into an array of characters with the spread operator.

Then we assign the returned array to strArr.

Therefore, strArr is  ['1', '2', '3', '4', '5'].

Conclusion

To get the same result as C# String.ToCharArray() with JavaScript, we can use the spread operator.