How to Convert Strings to Arrays in JavaScript (2 Easy Methods)

0

Learn how to efficiently convert strings to arrays in JavaScript. This guide explores various approaches for converting the string into an array. Basically we’ll create an array of characters from a string using JavaScript.


convert string into an array using JavaScript


We'll explore two ways to convert the string into an array using JavaScript.


The simplest way to convert a string to an array is using:


Way 1. Split Method.

JavaScript


    // With space.
    let string = "   Hello  welcome to  maketechstuff.com ";
    let array = string.split("");
    console.log("array: ", array);
    // Output:
    // array:  (40) [' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', ' ', 'w', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o', ' ', ' ', 'm', 'a', 'k', 'e', 't', 'e', 'c', 'h', 's', 't', 'u', 'f', 'f', '.', 'c', 'o', 'm', ' ']
    
  

Way 2. Array.from Method.

JavaScript


    let string = "   Hello  welcome to  maketechstuff.com ";
    let array = Array.from(string);
    console.log("array: ", array);
    //Output: 
    // array:  (40) [' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', ' ', 'w', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o', ' ', ' ', 'm', 'a', 'k', 'e', 't', 'e', 'c', 'h', 's', 't', 'u', 'f', 'f', '.', 'c', 'o', 'm', ' ']
  


The above method converts the string into an array, including spaces. This means the resulting array also contains empty elements (spaces).


If you want to convert the string into an array containing only non-empty elements, or convert the string to an array excluding spaces, here are some ways you can do it.


Way 1: Using trim(), replace() and split() Method.


JavaScript


    // Without space.
    let string = "   Hello  welcome to  maketechstuff.com ";
    let array = string.split("");
    let new_array  = [];
    for (let i = 0; i < array.length; i++) {
        const element = array[i];
        if(element !== " "){
            new_array.push(element)
        }
        
    }
    console.log("array: ", new_array);
    //Output:
    // array:  (31) ['H', 'e', 'l', 'l', 'o', 'w', 'e', 'l', 'c', 'o', 'm', 'e', 't', 'o', 'm', 'a', 'k', 'e', 't', 'e', 'c', 'h', 's', 't', 'u', 'f', 'f', '.', 'c', 'o', 'm']

  


Way 2: Using Split(), Push() Method and For loop.


JavaScript


    let string = "   Hello  welcome to  maketechstuff.com ";
    let array = string.trim().replace(/\s+/g, "").split("");
    
    console.log("array: ", array);

    // Output:
    // array:  (31) ['H', 'e', 'l', 'l', 'o', 'w', 'e', 'l', 'c', 'o', 'm', 'e', 't', 'o', 'm', 'a', 'k', 'e', 't', 'e', 'c', 'h', 's', 't', 'u', 'f', 'f', '.', 'c', 'o', 'm']
    
  


So that's how you can convert the string into an array using JavaScript. If you have any query or suggestions you can write in the comment section.


You may like:

  1. How to convert the given string into an array of words using JavaScript.
  2. How to count the characters present inside an string using JavaScript.
  3. How to count the words present inside a given string using JavaScript.
  4. How to create a character counter using JavaScript.


Tags

Post a Comment

0Comments
Post a Comment (0)
To Top