Member-only story

Javascript String Methods: A Cheat Sheet for Developer

Rahul Sharma (DevsMitra)
4 min readJun 10, 2022

--

Javascript String Methods: A Cheat Sheet for Developer | Rahul Sharma(DevsMitra)

Let’s understand javascript String functions and how to use them.

String.charAt()

Returns a string representing the character at the given index.

const str = "Hello World";
str.charAt(0); // "H"

String.charCodeAt()

Returns a number representing the UTF-16 code unit value of the character at the given index.

const str = "Hello World";
str.charCodeAt(0); // 72

String.concat()

Returns a new string containing the concatenation of the given strings.

const str = "Hello";
const str2 = " World";
str.concat(str2); // "Hello World"
console.log(`${str}${str2}`); // "Hello World"
console.log(str + str2); // "Hello World"

String.endsWith()

Returns true if the string ends with the given string, otherwise false.

const str = "Hello World";
str.endsWith("World"); // true

String.includes()

Returns true if the string contains the given string, otherwise false.

const str = "Hello World";
str.includes("World"); // true

String.indexOf()

Returns the index within the string of the first occurrence of the specified value, or -1 if not found.

const str = "Hello World";
str.indexOf("World"); // 6

String.lastIndexOf()

Returns the index within the string of the last occurrence of the specified value, or -1 if not found.

const str = "Hello World";
str.lastIndexOf("World"); // 6

String.match()

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

const str = "Hello World";
str.match(/[A-Z]/); // ["H"]

String.matchAll()

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

const str = "Hello World";
str.matchAll(/[A-Z]/g); // ["H", "W"]

--

--

Rahul Sharma (DevsMitra)
Rahul Sharma (DevsMitra)

Written by Rahul Sharma (DevsMitra)

I’m a technology enthusiast who does web development. Passionate to contribute to open-source projects and make cool products.

No responses yet

Write a response