Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To explode string in javascript, use split() method it will explode your string based upon your parameter you have passed in a split() method. let’s take a example

The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method’s call.

In the following example, we can able explode or split string by every char, space, and your desired string.

const str = 'Hear about the new restaurant called Karma, There’s no menu: You get what you deserve';

//  explode by every char
str.split('') // ['H', 'e', 'a', 'r', ' ', 'a', 'b', 'o', 'u', 't', ' ', 't', 'h', 'e', ' ', 'n', 'e', 'w', ' ', 'r', 'e', 's', 't', 'a', 'u', 'r', 'a', 'n', 't', ' ', 'c', 'a', 'l', 'l', 'e', 'd', ' ', 'K', 'a', 'r', 'm', 'a', ',', ' ', 'T', 'h', 'e', 'r', 'e', '’', 's', ' ', 'n', 'o', ' ', 'm', 'e', 'n', 'u', ':', ' ', 'Y', 'o', 'u', ' ', 'g', 'e', 't', ' ', 'w', 'h', 'a', 't', ' ', 'y', 'o', 'u', ' ', 'd', 'e', 's', 'e', 'r', 'v', 'e']

// explode by space
str.split(' ') // ['Hear', 'about', 'the', 'new', 'restaurant', 'called', 'Karma,', 'There’s', 'no', 'menu:', 'You', 'get', 'what', 'you', 'deserve']

// explode by comma
str.split(',') // ['Hear about the new restaurant called Karma', ' There’s no menu: You get what you deserve']

Today, I’m going to show you How do i explode string in javascript, as above mentioned here, I’m going to use split() method to explode string via desired seprator.

Let’s start today’s tutorial how do you explode string in javascript?

In this example, we will do

  1. Take example of split string by char
  2. Take example of split string by space
  3. Take example of split string by comma
// Take example of split string by char
let str1 = "infinitbility";
str1 = str1.split('');
console.log(str1); // ['i', 'n', 'f', 'i', 'n', 'i', 't', 'b', 'i', 'l', 'i', 't', 'y']

// Take example of split string by space
let str2 = "Infinitbility want to make differance";
str2 = str2.split(' ');
console.log(str2); //  ['Infinitbility', 'want', 'to', 'make', 'differance']

// Take example of split string by comma
let str3 = "infinitbility,aguidehub,sortoutcode";
str3 = str3.split(',');
console.log(str3); // ['infinitbility', 'aguidehub', 'sortoutcode']

In the above exampls, we have taken multiple string example and explode by diferant parameters, Let’s check the output.

JavaScript, explode string in javascript example
JavaScript, explode string in javascript example

I hope it’s help you, All the best 👍.