Open In App

What is split() Method in JavaScript ?

In JavaScript, the split() method is used to split a string into an array of substrings based on a specified separator. It allows you to break a string into smaller parts, which are then stored as elements in an array.

Syntax:

string.split(separator, limit)

Example: Here, the split() method is called on the str string with "," as the separator. This splits the string wherever a comma , is found, resulting in an array ["apple", "banana", "orange"], with each fruit name as an element.




const str = "apple,banana,orange";
const fruitsArray = str.split(",");
 
console.log(fruitsArray); // Output: ["apple", "banana", "orange"]

Output
[ 'apple', 'banana', 'orange' ]



Article Tags :