To split a string in JavaScript, you can use the split
method.
In this example,
str
with a value of "Hello World"
.split
method with a space as the delimiter.let str = 'Hello World';
let parts = str.split(' ');
console.log(parts);
[ 'Hello', 'World' ]
In this example,
str
with a value of "apple,banana,cherry"
.split
method with a comma as the delimiter.let str = 'apple,banana,cherry';
let parts = str.split(',');
console.log(parts);
[ 'apple', 'banana', 'cherry' ]
In this tutorial, we learned How to Split a String in JavaScript language with well detailed examples.