JavaScript Split
- The JavaScript
split()
method splits a string into an array of sub strings. - The split method takes two arguments and they’re both optional.
- One is Seperator and another one is limit
- Separator – determines the character to use for splitting the string.
- limit – is an integer specifying the number of splits.Items after this will not be included
javascript split string
Its upto you where and how to split.
let string = "Hi I'm Your New Friend"; let splitted = string.split(" "); console.log(splitted);
javascript split string into array
["Hi","I'm","Your","New","Friend"]
console.log(splitted[2]); // Your
javascript split string by comma ,
let string1 = "Hello, How are You today?"; let splitted1 = string1.split(","); console.log(splitted1);
["Hello","How are You today?"]
Let split a string without a space
let string2 = "Yesterday"; let splitted2 = string2.split(""); console.log(splitted2);
["Y", "e", "s", "t", "e", "r", "d", "a", "y"];
Let split the string by a letter
- It Takes letter away
let string3 = "Yesterday"; let splitted3 = string3.split("e"); console.log(splitted3);
["Y", "st", "rday"];
- limit is
1
let string4 = "Yesterday"; let splitted4 = string4.split("e",1); console.log(splitted4);
["Y"];