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

String methods JavaScript

JavaScript String .length


let v = "Hello World".length;
console.log('v =',v);

Output


v = 11

Steps Description

  1. Declare variable v
  2. Initialize variable v with value of string v = "Hello World".length
  3. String .length Calculates the length of a string by counting its characters and output it as a number.
  4. The value of v variable is displayed with the help of console.log inbuilt function

JavaScript String .concat() Method


let b = "Hello".concat(" ","World");
console.log('b =',b);

Output


b = Hello World

Steps Description

  1. Declare variable b
  2. Initialize variable b with value of three string b = "Hello".concat(" ","World")
  3. Multiple strings are concatenated to the first string by the concat() method.
  4. The value of b variable is displayed with the help of console.log inbuilt function

JavaScript String .replace() Method


let l = "Python is best language".replace('Python','JavaScript');
console.log('l =',l);

Output


l = JavaScript is best language

Steps Description

  1. Declare variable l
  2. Initialize variable l with value of string l = "Python is best language".replace('Python','JavaScritp')
  3. replace() method if a word or character is found in the string after searching, then it works to replace that word or character.
  4. The value of l variable is displayed with the help of console.log inbuilt function

JavaScript String .charAt() Method


let a = "Hello".charAt(4);
console.log('a =',a);

Output


a = o

Steps Description

  1. Declare variable a
  2. Initialize variable a with value of string a="Hello"
  3. The charAt() method returns the character of the given string.
  4. The value of a variable is displayed with the help of console.log inbuilt function

JavaScript String .search() Method


let m = "Hello World".search('r');
console.log('m =',m);

Output


m = 8

Steps Description

  1. Declare variable m
  2. Initialize variable m with value of string m = "Hello World".search('r')
  3. search() method is used to search a string Will print the index number of a word or character if found in the given string word or character
  4. The value of m variable is displayed with the help of console.log inbuilt function

JavaScript String .indexOf() Method


let f = "Hello World".indexOf("World");
console.log('f =',f);

Output


f = 6

Steps Description

  1. Declare variable f
  2. Initialize variable f with value of string f = "Hello World".indexOf("World")
  3. indexOf() method will print the number of the index if the word or character is found in the string. will print -1 if not found
  4. The value of f variable is displayed with the help of console.log inbuilt function

JavaScript String .lastIndexOf() Method


let g = "Hello in World in JavaScript".lastIndexOf("in");
console.log('g =',g);

Output


g = 15

Steps Description

  1. Declare variable g
  2. Initialize variable g with value of string g = "Hello in World in JavaScript".lastIndexOf("in")
  3. lastIndexOf() method is used to find a word or letter from the end of the string. When searching for a word or character in a string, if the word or character is found, its index number will be printed. will print -1 if not found
  4. The value of g variable is displayed with the help of console.log inbuilt function

JavaScript String .includes() Method


let e = "Hello World".includes("World");
console.log('e =',e);

Output


e = true

Steps Description

  1. Declare variable e
  2. Initialize variable e with value of string "Hello World".includes("World")
  3. includes() method is used to search in a string. Returns true if the word or letter is found in the given string, otherwise false.
  4. The value of e variable is displayed as a return boolean value with the help of console.log inbuilt function

JavaScript String .startsWith() Method


let c = "Hello my friend".startsWith("My");
console.log('c =',c);

Output


c = false

Steps Description

  1. Declare variable c
  2. Initialize variable c with value of string "Hello".startsWith("e")
  3. startsWith() method is used to find the first word or character of a string. returns boolean value
  4. The value of c variable is displayed with the help of console.log inbuilt function

JavaScript String .endsWith() Method


let d = "World".endsWith("d");
console.log('d =',d);

Output


d = true

Steps Description

  1. Declare variable d
  2. Initialize variable d with value of string d = "World".endsWith("d")
  3. endsWith() method is used to find the last word or character of a string. returns boolean value
  4. The value of d variable is displayed with the help of console.log inbuilt function

JavaScript String .trim() Method


let s = "  Hello World   ".trim();
console.log('s =',s);

Output


s = Hello World

Steps Description

  1. Declare variable s
  2. Initialize variable s with value of string s = " Hello World ".trim()
  3. trim() Method If there is one or more spaces between the first and last of the string, the trim method works to remove it.
  4. The value of s variable is displayed with the help of console.log inbuilt function

JavaScript String .trimStart() Method


let t = "  Hello World   ".trimStart();
console.log('t =',t);

Output


t = Hello World 

Steps Description

  1. Declare variable t
  2. Initialize variable t with value of string t = " Hello World ".trimStart()
  3. trimStart() Method If a string contains one or more spaces at the beginning, the trimStart method serves to remove it.
  4. The value of t variable is displayed with the help of console.log inbuilt function

JavaScript String .trimEnd() Method


let u = "  Hello World   ".trimEnd();
console.log('u =',u);

Output


u =   Hello World

Steps Description

  1. Declare variable u
  2. Initialize variable u with value of string u = " Hello World ".trimEnd()
  3. trimEnd() Method If a string contains one or more spaces at the end, the trimEnd method serves to remove it.
  4. The value of u variable is displayed with the help of console.log inbuilt function

JavaScript String .padStart() Method


let i = "Hello World".padStart(12,"1");
console.log('i =',i);

Output


i = 1Hello World

Steps Description

  1. Declare variable i
  2. Initialize variable i with value of string i = "Hello World".padStart(12,"1")
  3. padStart() method can add a character, number, or symbol to the beginning of a string.
  4. The value of i variable is displayed with the help of console.log inbuilt function

JavaScript String .padEnd() Method


let j = "Hello World JavaScript".padEnd(23,"|");
console.log('j =',j);

Output


j = Hello World JavaScript|

Steps Description

  1. Declare variable j
  2. Initialize variable j with value of string j = "Hello World JavaScript".padEnd(23,"|")
  3. padEnd() method adds a character, number, or symbol to the end of a string.
  4. The value of i variable is displayed with the help of console.log inbuilt function

JavaScript String .toUpperCase() Method


let r = "Hello World".toUpperCase();
console.log('r =',r);

Output


r = HELLO WORLD

Steps Description

  1. Declare variable r
  2. Initialize variable r with value of string r = "Hello World".toUpperCase()
  3. toUpperCase() method converts all words or characters in a string to uppercase.
  4. The value of r variable is displayed with the help of console.log inbuilt function

JavaScript String .toLowerCase() Method


let q = "Hello World".toLowerCase();
console.log('q =',q);

Output


q = hello world

Steps Description

  1. Declare variable q
  2. Initialize variable q with value of string q = "Hello World".toLowerCase()
  3. toLowerCase() method Converts all words or characters in a string to lowercase.
  4. The value of q variable is displayed with the help of console.log inbuilt function

JavaScript String .slice() Method


let n = "Hello World JavaScript Language".slice(1,5);
console.log('n =',n);

Output


n = ello

Steps Description

  1. Declare variable n
  2. Initialize variable n with value of string n = "Hello World JavaScript Language".slice(1,5)
  3. slice() method can pick up words, letters, or words from one set position to another set position in a string.
  4. The value of n variable is displayed with the help of console.log inbuilt function

JavaScript String .split() Method


let o = "Hello World JavaScript Language".split("World");
console.log('o =',o);

Output


o = [ 'Hello ', ' JavaScript Language' ]

Steps Description

  1. Declare variable o
  2. Initialize variable o with value of string o = "Hello World JavaScript Language".split("World")
  3. split() method will search for a given word, character, or symbol in a string and convert it into an array of commas.
  4. The value of o variable is displayed with the help of console.log inbuilt function

JavaScript String .substring() Method


let p = "Hello World".substring(1,7);
console.log('p =',p);

Output


p = ello W

Steps Description

  1. Declare variable p
  2. Initialize variable p with value of string p = "Hello World".substring(1,7)
  3. substring() Method method can pick up words, letters, or words from one set position to another set position in a string
  4. The value of p variable is displayed with the help of console.log inbuilt function

JavaScript String .match() Method


let h = "Hello World in JavaScript Hello World".match(/World/g);
console.log('h =',h);

Output


h = [ 'World', 'World' ]

Steps Description

  1. Declare variable h
  2. Initialize variable h with value of string h = "Hello World in JavaScript Hello World".match(/World/g)
  3. If a word or character is found in the string, the match() method will create an array.
    else will print null
  4. The value of h variable is displayed with the help of console.log inbuilt function

JavaScript String .repeat() Method


let k = "Hello World ".repeat(3);
console.log('k =',k);

Output


k = Hello World Hello World Hello World

Steps Description

  1. Declare variable k
  2. Initialize variable k with value of string k = "Hello World ".repeat(3)
  3. repeat() Method Whatever words or letters are written in the string, they can be repeated again and again.
  4. The value of k variable is displayed with the help of console.log inbuilt function

JavaScript String .charCodeAt() Method


let w = "Hello World".charCodeAt(4);
console.log('w =',w);

Output


w = 111

Steps Description

  1. Declare variable w
  2. Initialize variable w with value of string w = "Hello World".charCodeAt(4)
  3. charCodeAt() method returns the ascii value of the character at the index of the character in the string.
  4. The value of w variable is displayed with the help of console.log inbuilt function

JavaScript String fromCharCode() Method


let x = String.fromCharCode(65,120)
console.log('x =',x);

Output


x = Ax

Steps Description

  1. Declare variable x
  2. Initialize variable x with value of number x = String.fromCharCode(65,120)
  3. String fromCharCode() method prints the given ascii number in character
  4. The value of x variable is displayed with the help of console.log inbuilt function