Write the usage of split() method in javascript?

JavascriptFront End TechnologyObject Oriented Programming

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
raja
Published on 19-Sep-2019 07:32:40
Advertisements