JavaScript split() Method

Introduction

The split() method is one of the most useful string methods in JavaScript. It is used to divide a string into multiple parts and convert it into an array.

What is JavaScript split() Method?

The split() method divides a string into an array based on a specified separator.

Syntax of split()


string.split(separator, limit);

Explanation:

split() accepts two parameters.

  • separator: Defines where to split the string.
  • limit: Limits the number of returned elements.

Example:


let fruits = "Apple,Banana,Mango";
let result = fruits.split(",");
console.log(result);

Output:

[“Apple”,”Banana”,”Mango”]

Note: The comma acts as a separator.

Examples of split() Method

You will see some examples of the split() method.

Example 1: Split by Space


let text = "Learn JavaScript";
console.log(text.split(" "));

Output:

[“Learn”,”JavaScript”]

Example 2: Split by Comma


let fruits = "Apple,Banana,Mango";
console.log(fruits.split(","));

Output:

[“Apple”,”Banana”,”Mango”]

Example 3: Split Characters


let word="Hello";
console.log(word.split(""));

Output:

[“H”,”e”,”l”,”l”,”o”]

Example 4: Split with Limit


let word="H e l l o";
console.log(str.split(" ",2));

Output:

[“H”,”e”]

Real-Life Example of split()

Suppose a user enters skills: HTML,CSS,JavaScript

Convert into an array:


let skills = "HTML,CSS,JavaScript";
let result = skills.split(",");
console.log(result);

Output:

[“HTML”,”CSS”,”JavaScript”]

Common Mistakes in split()

Mistake 1: Forgetting the Separator

If you forget the separator, then no splitting occurs.


name = "John";
let result = name.split();
console.log(result);

Output:

[“John”]

Correct Way:


name = "John";
let result = name.split("");
console.log(result);

Output:

[“J”, “o”, “h”, “n”]

Mistake 2: Wrong Separator

Suppose you use the wrong separator.

Wrong:


name = "John Taylor";
let result = name.split(",");
console.log(result);

Output:

[ ‘John Taylor’ ]

Correct separator:


name = "John Taylor";
let result = name.split(" ");
console.log(result);

Output:

[ ‘John’, ‘Taylor’ ]

Difference Between split() and slice()

Feature split() slice()
Return Type Array String
Purpose Divide string Extract portion
Parameter Separator required Start/end index
Syntax string.split(separator) string.slice(start, end)
Example let str = "HTML,CSS,JS";
console.log(str.split(","));
// ["HTML","CSS","JS"]
let str = "JavaScript";
console.log(str.slice(0,4));
// "Java"

JavaScript split() Method – Interview Questions

Q 1: What does split() return?
Ans: Array
Q 2: Syntax of split()?
Ans: string.split(separator, limit)
Q 3: Difference between split() and join()?
Ans: split() → String to array
join() → Array to string
Q 4: Can split() be used without separator?
Ans: Yes
Q 5: What will be output?
Ans:
"Hello".split("")

Conclusion

The JavaScript split() method is essential for converting strings into arrays. It is widely used in form handling, APIs, CSV processing, and text manipulation. Understanding split() improves JavaScript fundamentals and helps solve coding interview problems.

Related JavaScript Tutorials