How to use JavaScript String split()
String split
On this page
String split() method
Program to use JavaScript String split() method
let a = "Hello World JavaScript";
let b = a.split("");
console.log(b);
Output
[
'w', 'a', 'n', 't', ' ', 't',
'o', ' ', 'c', 'o', 'd', 'e',
' ', 'j', 'a', 'v', 'a', 's',
'c', 'r', 'i', 'p', 't', ' ',
'w', 'i', 't', 'h', ' ', 'm',
'e'
]
Steps Description
- Declare two variable a and b
- Initialize two variable a and b with value of string a = “Hello World JavaScript”; and b
- The split() method converts a string into an array.
- If we split a string without space with the help of split method, it will turn into a string array
- The value of b variable is displayed with the help of console.log inbuilt function
String split() method
Program to use JavaScript String split() method
let c = "Hello World JavaScript";
let d = c.split(" ");
console.log(d);
Output
[ 'Hello', 'World', 'JavaScript' ]
Steps Description
- Declare two variable c and d
- Initialize two variable c and d with value of string c = “Hello World JavaScript”; and d
- The split() method converts a string into an array.
- The value of d variable is displayed with the help of console.log inbuilt function
String split() method
Program to use JavaScript String split() method
let e = "Hello. World. JavaScript";
let f = e.split(".");
console.log(f);
Output
[ 'Hello ', ' World ', ' JavaScript' ]
Steps Description
- Declare two variable e and f
- Initialize two variable e and f with value of string e = “Hello. World. JavaScript” and f
- The split() method converts a string into an array.
- The value of f variable is displayed with the help of console.log inbuilt function
String split() method
Program to use JavaScript String split() method
let g = "Hello World JavaScript";
let h = g.split(" ");
console.log(h[2]);
Output
javascript
Steps Description
- Declare two variable g and h
- Initialize two variable g and h with value of string g = “Hello World JavaScript” and h
- The split() method converts a string into an array.
- The value of h variable is displayed with the help of console.log inbuilt function