JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS String Methods


📘 JavaScript String Methods – Manipulating and Formatting Strings

JavaScript provides many string methods that let you transform, extract, search, or replace text. These methods do not modify the original string (because strings are immutable in JS) but return a new string instead.


🔹 Commonly Used String Methods

Method Description Example
slice(start, end) Extracts part of a string "Hello".slice(1, 4)"ell"
substring(start, end) Similar to slice(), but can't take negative indexes "Hello".substring(1, 4)"ell"
substr(start, length) Returns length chars from start index (deprecated) "Hello".substr(1, 3)"ell"
replace(find, replace) Replaces first match with new string "Hi John".replace("John", "Sam")"Hi Sam"
replaceAll(find, replace) Replaces all matches "a-a-a".replaceAll("a", "b")"b-b-b"
toUpperCase() Converts all letters to uppercase "hello".toUpperCase()"HELLO"
toLowerCase() Converts all letters to lowercase "HELLO".toLowerCase()"hello"
trim() Removes whitespace from both ends " hello ".trim()"hello"
padStart(len, char) Pads the beginning of the string "5".padStart(3, "0")"005"
padEnd(len, char) Pads the end of the string "5".padEnd(3, "*")"5**"
repeat(n) Repeats the string n times "hi".repeat(3)"hihihi"
split(delimiter) Splits string into array based on delimiter "a,b,c".split(",")["a", "b", "c"]

🔹 Examples

"JavaScript".slice(4);          // "Script"
"JavaScript".substring(0, 4);   // "Java"
"  hello  ".trim();             // "hello"
"5".padStart(3, "0");           // "005"
"abcabc".replaceAll("a", "*");  // "*bc*bc"

Practice Questions

Q1. How do you extract "Script" from the string "JavaScript" using slice()?

Q2. How do you replace "Hello" with "Hi" in the string "Hello World"?

Q3. How do you convert "good morning" to uppercase?

Q4. How do you trim the spaces from both sides of " welcome "?

Q5. Which method will split "apple,banana,grape" into an array of fruits?

Q6. How do you pad the string "9" to become "009" using a string method?

Q7. What method can you use to repeat "ha" 4 times to form "hahahaha"?

Q8. How do you replace all occurrences of "a" with "*", in "a-a-a"?

Q9. How do you extract the substring "ava" from "JavaScript" using substring()?

Q10. What method will convert "JAVASCRIPT" to lowercase letters?


JS String Methods Quiz

Q1: What does "Welcome".slice(3) return?

A. "com"
B. "lcome"
C. "come"
D. "lco"

Q2: What does "Hi John".replace("John", "Mike") return?

A. "Hi Mike"
B. "Hi John Mike"
C. "Mike John"
D. "Hi"

Q3: Which method removes whitespace from both sides of a string?

A. clear()
B. strip()
C. trim()
D. cut()

Q4: Which method converts "hello" to "HELLO"?

A. toUpper()
B. toUpperCase()
C. upperCase()
D. upper()

Q5: What does "10".padStart(4, "0") return?

A. "0010"
B. "1000"
C. "0100"
D. "10"

Q6: What is the output of "js".repeat(3)?

A. "js"
B. "jsjs"
C. "jsjsjs"
D. "3js"

Q7: Which of the following is used to split "a,b,c" into an array?

A. explode(",")
B. split(",")
C. splice(",")
D. cut(",")

Q8: Which method replaces all matches in a string?

A. replace()
B. replaceAll()
C. swapAll()
D. substituteAll()

Q9: What does "abcabc".replaceAll("a", "*") return?

A. "*bc*bc"
B. "abcabc"
C. "**bc"
D. "bc*bc"

Q10: Which method extracts a string portion based on character position?

A. cut()
B. slice()
C. shrink()
D. splice()

Go Back Top