Write the usage of split() method in javascript?


The split([separator, [limit]]) method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.

Example usage of split method

let a = "hello,hi,bonjour,namaste";
let greetings = a.split(',');
console.log(greetings)

Output

[ 'hello', 'hi', 'bonjour', 'namaste' ]

Note that the commas were removed here. Any separator provided will be removed.

If separator is an empty string, str is converted to an array having one element for each character of str.

Example

let a = "hello";
console.log(a.split(""))

Output

[ 'h', 'e', 'l', 'l', 'o' ]

If no separator is provided, the string is returned as is.

Example

let a = "hello world"
console.log(a.split())

Output

hello world

Updated on: 19-Sep-2019

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements