JavaScript String Methods Cheat Sheet PDF

1). String.indexOf()

This method will return the index within the string of the first occurrence of the specified string value. And it will return -1 if not found.

const str = "Geeks Help";
let strIndex = str.indexOf("Help");
console.log(strIndex);

Output: 6


2). String.lastIndexOf()

Returns the index within the string of the last occurrence of the specified value and it will return -1 if not found.

const str = "Geeks Help";
let lastIndex = str.lastIndexOf("Geeks");
console.log(lastIndex);

Output: 0


3). String.match()

Returns a list of matches of a regular expression against a string.

const str = "Hello World";
let strMatch = str.match(/[A-Z]/);
console.log(strMatch);

Output: ['H', index: 0, input: 'Hello World', groups: undefined]


4). String.charAt()

Returns a string representing the character at the given index.

const str = "Hello World";
let strChar = str.charAt(0);
console.log(strChar);

Output: H


5). String.concat()

It is used for string concatenation and it returns a new string.

const str1 = "Geeks ";
const str2 = "Help";
let newString = str1.concat(str2);
console.log(newString);

Output: Geeks Help


6). String.endsWith()

It returns true if the string contains the given string value otherwise it returns false

const siteStr= "Geeks Help";
const containStr = siteStr.endsWith("World");
console.log(containStr);

Output: false


7). String.toLowerCase()

It is used to convert the string to lowercase characters and returns a new string.

const str = "Geeks Help";
let lowerStr = str.toLowerCase();
console.log(lowerStr);

Output: geeks help


8). String.toUpperCase()

It is used to convert the string to uppercase characters and returns a new string.

const str = "Geeks Help";
let upperStr = str.toUpperCase();
console.log(upperStr);

Output: GEEKS HELP


9). String.toString()

This string method returns the string representation of the specified object.

const str = new String("Geeks Help");
let strToString= str.toString();
console.log(strToString);

Output: Geeks Help


10). String.matchAll()

This string method returns a list of matches of a regular expression against a string.

const str = "Hello World";
let strMatch = str.matchAll(/[A-Z]/g);
console.log(strMatch);

Output: Object [RegExp String Iterator] {}


11). String.repeat()

This method creates copies of the string according to the specified number and returns a new string.

const str = "Geeks ";
let repeatStr = str.repeat(3);
console.log(repeatStr);

Output: Geeks Geeks Geeks


12). String.replace()

It replaces the first match of a regular expression in the string replace by a new character and returns a new string.

const str = "Geeks Help";
let replaceStr = str.replace("e", "#");
console.log(replaceStr);

Output: G#eks Help


13). String.slice()

This method returns a new string containing the characters of the string from the given index to the end of the string.

const str = "Geeks Help";
let strSlice = str.slice(6);
console.log(strSlice);

Output: Help


14). String.split()

This method returns an array of strings split at the given index.

const str = "Geeks Help";
let strSplit = str.split(" ");
console.log(strSplit);

Output: [ 'Geeks', 'Help' ]


15). String.startsWith()

This string method returns true if the string starts with the given string, otherwise, it returns false.

const str = "Geeks Help";
let strStarts = str.startsWith("Raju");
console.log(strStarts);

Output: false


16). String.trim()

The trim() method removes whitespace from both sides of a string. And it returns a new string.

const str = "  Geeks Help";
let strTrim = str.trim();
console.log(strTrim);

Output: Geeks Help


17). String.trimStart()

This string method removes whitespace from the beginning of a string.

const str = "  Geeks Help ";
let trimStart = str.trimStart();
console.log(trimStart);

Output: Geeks Help


18). String.trimEnd()

This string method removes whitespace from the end of a string.

const str = "  Geeks Help  ";
let trimEnd = str.trimEnd();
console.log(trimEnd);

Output:      Geeks Help


JavaScript String Methods Cheat Sheet PDF: Download Now

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad