JavaScript String split() Method (original) (raw)

Last Updated : 10 Jan, 2025

The JavaScript split() method divides a string into an array of substrings based on a specified separator, such as a character, string, or regular expression. It returns the array of split substrings, allowing manipulation of the original string data in various ways.

JavaScript `

let str = "Hello and Welcome to GeeksforGeeks"; let words = str.split(" "); console.log(words);

`

**Syntax

str.split( separator, limit );

Parameters

**Return Value

This function returns an array of strings that is formed after splitting the given string at each point where the separator occurs.

**Example: The split() function divides the string “Geeks for Geeks” at “for”, creating an array with two parts: [‘Geeks ‘, ‘ Geeks’]. It separates based on the given delimiter.

JavaScript `

// JavaScript Program to illustrate split() method let str = 'Geeks for Geeks' let array = str.split("for"); console.log(array);

`

Output

[ 'Geeks ', ' Geeks' ]

**Example: The function split() creates an array of strings by splitting str wherever ” ” occurs.

JavaScript `

// JavaScript Program to illustrate split() function let str = 'It is a 5r&e@@t Day.' let array = str.split(" "); console.log(array);

`

Output

[ 'It', 'is', 'a', '5r&e@@t', 'Day.' ]

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

**Supported Browsers