
How to split up a string in JavaScript using the split function
Sometimes in JavaScript, you might have a sentence with different words in it and you need to find out a way to split this sentence up into individual words.
The Method of Split Function.
There is a method on the string prototype called split and we can use this method to split up a string easily. First, create a const variable which going to contain the sentence as a string.
const sentence = "my name is ayon";
After that, we will create another const variable which going to contain a list of individual words. To do that we’re going to use the split function.
const sentence = "my name is ayon"; const words = sentence.split(" "); console.log(words);
So, the split function going to take the sentence and separate it out into different tokens based on what you pass the split argument. So, I’m going to put space because I want to split all the spaces. And now if I were to go ahead and console log this, notice that we get back an array that has a bunch of different words in it.
Now we can do what we want to do with this array. So, this is the split function in JavaScript.