Blog

Methods

JS

20+ must-know String methods

Comprehensive study on string methods

Ilavenil Diagarajane

Ilavenil Diagarajane

Oct 11, 2022 — Updated Dec 8, 2022 · 5 min read

This method returns the number of characters and spaces in a string.


_4
let string1 = "JavaScript j";
_4
let len = string1.length;
_4
_4
console.log(len); // 12

  • The replace() method returns a new string

  • It replaces with the specified string or regex


_8
const p = "The quick brown fox jumps over the lazy dog.";
_8
_8
console.log(p.replace("dog", "monkey"));
_8
// "The quick brown fox jumps over the lazy monkey."
_8
_8
const regex = /Dog/i;
_8
console.log(p.replace(regex, "ferret"));
_8
// "The quick brown fox jumps over the lazy ferret."

Returns the index of value first occurrence of substring.


_13
var str = "JavaScript is the world's most misunderstood programming language.";
_13
_13
var index1 = str.indexOf("language");
_13
console.log(index1); // 57
_13
_13
var index2 = str.indexOf("p");
_13
console.log(index2); // 8
_13
_13
var index3 = str.indexOf("p", 9);
_13
console.log(index3); // 45
_13
_13
var index4 = str.indexOf("Python");
_13
console.log(index4); // -1

Returns the index of the last occurrence of the specified substring.


_5
var str = "JavaScript is the world's most misunderstood programming language.";
_5
var substr = "m";
_5
_5
var result = str.lastIndexOf(substr);
_5
console.log(result); // 52

Returns true if a string begins with specified character(s). If not, it returns false.


_8
const message =
_8
"JavaScript is the world's most misunderstood programming language";
_8
_8
let result = message.startsWith("JavaScript");
_8
console.log(result); // true
_8
_8
result = message.startsWith("Script");
_8
console.log(result); // false

Returns true if a string ends with the specified string. If not, the method returns false.


_8
let sentence =
_8
"JavaScript is the world's most misunderstood programming language";
_8
_8
let check = sentence.endsWith("language");
_8
console.log(check); // true
_8
_8
let check1 = sentence.endsWith("is");
_8
console.log(check1); // false

The method returns a string in uppercase form.


_6
let message =
_6
"JavaScript is the world's most misunderstood programming language";
_6
_6
const upperMessage = message.toUpperCase();
_6
console.log(upperMessage);
_6
// JAVASCRIPT IS THE WORLD'S MOST MISUNDERSTOOD PROGRAMMING LANGUAGE

The method returns a string in lowercase form.


_6
let message =
_6
"JAVASCRIPT IS THE WORLD'S MOST MISUNDERSTOOD PROGRAMMING LANGUAGE";
_6
_6
const lowerMessage = message.toLowerCase();
_6
console.log(lowerMessage);
_6
// javascript is the world's most misunderstood programming language

  • The method checks if one string can be found inside another string
  • If it presents it will return true else false
  • It performs a case-sensitive search to determine inside or not

_5
const message =
_5
"JavaScript is the world's most misunderstood programming language";
_5
_5
let result = message.includes("Java");
_5
console.log(result); // true

This method returns a specified string repeated a given number of times.


_4
const message = "JavaScript ";
_4
_4
const result = message.repeat(3);
_4
console.log(result); // JavaScript JavaScript JavaScript

This method returns the character of the specified index from a string.


_4
const string = "javascript";
_4
_4
let index1 = string.charAt(1);
_4
console.log("Character at index 1 is " + index1); // Character at index 1 is a

Returns Unicode of the character at the given index


_5
const string = "javascript";
_5
_5
let index1 = string.charCodeAt(1);
_5
console.log("Unicode of Character at index 1 is " + index1);
_5
// Unicode of the Character at index 1 is 97

The method returns a string created from the specified sequence of UTF-16 code units.


_2
let string1 = String.fromCharCode(72, 69, 76, 76, 79);
_2
console.log(string1); // HELLO

The method returns the part of the string between the start and end indexes, or to the end of the string.


_4
const message = "javascript";
_4
_4
console.log(message.substring(1, 3)); // "av"
_4
console.log(message.substring(2)); // "vascript"

Returns the matching string with a regex pattern.


_5
const message = " Welcome to JavaScript!";
_5
const regex = /[A-Z]/g;
_5
const result = message.match(regex);
_5
_5
console.log(result); // ["W", "J"]

Returns an iterator of all matching a string with a regex pattern.


_7
const regexp = /t(e)(st(\d?))/g;
_7
const str = "test1test2";
_7
_7
const array = [...str.matchAll(regexp)];
_7
_7
console.log(array[0]); // Array ["test1", "e", "st1", "1"]
_7
console.log(array[1]); // Array ["test2", "e", "st2", "2"]

String divided into the list of substring


_7
const string = "The quick brown fox jumps over the lazy dog.";
_7
_7
console.log(string.split());
_7
// ["The quick brown fox jumps over the lazy dog."]
_7
_7
console.log(string.split(" "));
_7
// ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']

The method extracts a section of a string and returns it as a new string, without modifying the original string.


_13
const str = "The quick brown fox jumps over the lazy dog.";
_13
_13
console.log(str.slice(31));
_13
// "the lazy dog."
_13
_13
console.log(str.slice(4, 19));
_13
// "quick brown fox"
_13
_13
console.log(str.slice(-4));
_13
// "dog."
_13
_13
console.log(str.slice(-9, -5));
_13
// "lazy"

The method removes whitespace from both ends of a string and returns a new string, without modifying the original string.


_3
const greeting = " Hello world! ";
_3
console.log(greeting.trim());
_3
// "Hello world!";

The method removes whitespace from the end of a string.


_3
const greeting = " Hello world! ";
_3
console.log(greeting.trimEnd());
_3
// " Hello world!";

The method removes whitespace from the beginning of a string.


_3
const greeting = " Hello world! ";
_3
console.log(greeting.trimStart());
_3
// "Hello world! ";

@ragavkumarv
swipe to next ➡️