How to use JavaScript String split()

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

  1. Declare two variable a and b
  2. Initialize two variable a and b with value of string a = "Hello World JavaScript"; and b
  3. The split() method converts a string into an array.
  4. If we split a string without space with the help of split method, it will turn into a string array
  5. The value of b variable is displayed with the help of console.log inbuilt function

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

  1. Declare two variable c and d
  2. Initialize two variable c and d with value of string c = "Hello World JavaScript"; and d
  3. The split() method converts a string into an array.
  4. The value of d variable is displayed with the help of console.log inbuilt function

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

  1. Declare two variable e and f
  2. Initialize two variable e and f with value of string e = "Hello. World. JavaScript" and f
  3. The split() method converts a string into an array.
  4. The value of f variable is displayed with the help of console.log inbuilt function

Program to use JavaScript String split() method


let g = "Hello World JavaScript";

let h = g.split(" ");

console.log(h[2]);

Output


javascript

Steps Description

  1. Declare two variable g and h
  2. Initialize two variable g and h with value of string g = "Hello World JavaScript" and h
  3. The split() method converts a string into an array.
  4. The value of h variable is displayed with the help of console.log inbuilt function