;

How to Get a Character Array From a String in Javascript


Tutorialsrack 12/04/2022 Jquery Javascript

In this article, you’ll learn how to get a character array from a string in Javascript. There are various ways to get a character array from a string. The string in JavaScript can be converted into a character array by using the split() function, Array.from() function, and using the spread(...) operator.

Here are some examples to get a character array from a string in Javascript.

Example 1: Using split() Function

In this example, we used the split() function to get a character array from a string. The split() function is used to split a string from a specific character. Let's take an example:

console.log('Hello Tutorialsrack'.split(''));
//Output ==> ["H", "e", "l", "l", "o", " ", "T", "u", "t", "o", "r", "i", "a", "l", "s", "r", "a", "c", "k"]


console.log('𝟙𝟚𝟛'.split(/(?!$)/u));
//Output ==> ["𝟙", "𝟚", "𝟛"]

console.log('😎😜🙃'.split(/(?!$)/u));
//Output ==> ["😎", "😜", "🙃"]

Example 2: Using Array.from() Function

In this example, we used the Array.from() function to get the character array from a string. The Array.from() in javascript is a static method used to create a new, shallow-copied Array instance from an array-like or iterable object. Let's take an example:

console.log(Array.from('Hello Tutorialsrack'));
//Output ==> ["H", "e", "l", "l", "o", " ", "T", "u", "t", "o", "r", "i", "a", "l", "s", "r", "a", "c", "k"]

console.log(Array.from('𝟙𝟚𝟛'));
//Output ==> ["𝟙", "𝟚", "𝟛"]

console.log(Array.from('😎😜🙃'));
//Output ==> ["😎", "😜", "🙃"]

Example 3: Using Spread(...) Operator

In this example, we used the spread(...) operator to get the character array from a string. In the Javascript ES6 version, a spread operator was introduced that consists of three dots (...).  Let's take an example:

console.log([...'Hello Tutorialsrack']);
//Output ==> ["H", "e", "l", "l", "o", " ", "T", "u", "t", "o", "r", "i", "a", "l", "s", "r", "a", "c", "k"]

console.log([...'𝟙𝟚𝟛']);
//Output ==> ["𝟙", "𝟚", "𝟛"]

console.log([...'😎😜🙃']);
//Output ==> ["😎", "😜", "🙃"]

I hope this article will help you to understand how to convert a string to a boolean in javascript.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags