JavaScript String
Manipulation
A JAVASCRIPT STRING IS ZERO OR MORE CHARACTERS WRITTEN INSIDE
QUOTES.
YOU CAN USE SINGLE OR DOUBLE QUOTES: ‘ ‘ , “ “
let name= “Nielit Kurukshetra“ ;
let str =‘NIELIT’;
Basic String Methods
Method Description Example
Returns the length of the
length "hello".length → 5
string
Returns the character at a
charAt(index) "hello".charAt(1) → "e"
specified index
Returns the index of the first
indexOf(substring) "hello".indexOf("l") → 2
occurrence
Returns the index of the last
lastIndexOf(substring) "hello".lastIndexOf("l") → 3
occurrence
✂️Extracting Substrings
Method Description Example
slice(start, end) Extracts a part of a string "hello".slice(1, 4) → "ell"
Similar to slice but no
substring(start, end) "hello".substring(1, 4) → "ell"
negative indexes
Extracts part of a string based
substr(start, length) "hello".substr(1, 3) → "ell"
on length
🔍 Searching in Strings
Method Description Example
includes(substring) Checks if substring exists "hello".includes("he") → true
Checks if string starts with
startsWith(substring) "hello".startsWith("he") → true
value
Checks if string ends with
endsWith(substring) "hello".endsWith("lo") → true
value
🔄 Modifying Strings
Method Description Example
toUpperCase() Converts to uppercase "hello".toUpperCase() → "HELLO"
toLowerCase() Converts to lowercase "HELLO".toLowerCase() → "hello"
Removes whitespace from both
trim() " hello ".trim() → "hello"
ends
"hello world".replace("world",
replace(old, new) Replaces first occurrence
"friend") → "hello friend"
"a-b-a".replaceAll("a", "x") → "x-b-
replaceAll(old, new) Replaces all occurrences
x"
🧱 Combining and Splitting Strings
Method Description Example
"Hello".concat(" ", "World") →
concat() Joins two or more strings
"Hello World"
repeat(n) Repeats the string n times "ha".repeat(3) → "hahaha"
Template Strings / Template Literals
let firstName = "John";
let lastName = "Doe";
let text = `Welcome ${firstName}, ${lastName}!`;