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 Search


📘 JavaScript String Search – Finding Substrings in Text

JavaScript provides several string search methods to locate specific text within a string. These help determine if, where, and how many times a substring exists.

All search methods are case-sensitive by default.


🔹 Common Search Methods

Method Description Example
indexOf(searchValue) Returns the first index of a match, or -1 if not found "hello".indexOf("l") → 2
lastIndexOf(searchValue) Returns the last index of a match, or -1 "hello".lastIndexOf("l") → 3
search(regexp) Searches using a regex (returns first match index or -1) "hello".search("e") → 1
includes(searchValue) Returns true if found, false otherwise "hello".includes("he") → true
startsWith(searchValue) Returns true if string starts with the value "hello".startsWith("he") → true
endsWith(searchValue) Returns true if string ends with the value "hello".endsWith("lo") → true

🔹 Examples

"apple".indexOf("p");         // 1
"apple".lastIndexOf("p");     // 2
"apple".search("l");          // 3
"apple".includes("ap");       // true
"apple".startsWith("a");      // true
"apple".endsWith("e");        // true

Practice Questions

Q1. How do you find the position of the first occurrence of "o" in "hello world"?

Q2. How do you find the index of the last "l" in "hello"?

Q3. Which method would you use to check if "apple" starts with "ap"?

Q4. How do you test if the string "coding" includes the substring "ing"?

Q5. How do you check if a string ends with "!"?

Q6. What does "banana".indexOf("na") return?

Q7. What does "banana".lastIndexOf("na") return?

Q8. How do you search for "e" using a regular expression in the string "elephant"?

Q9. How do you confirm if the word "JavaScript" includes the word "Script"?

Q10. How do you check whether "Hello" starts with "H" and ends with "o"?


JS String Search Quiz

Q1: What is the output of "hello".indexOf("l")?

A. 1
B. 2
C. 3
D. -1

Q2: What does "hello".lastIndexOf("l") return?

A. 2
B. 3
C. 1
D. -1

Q3: Which method checks if a string contains a given value?

A. has()
B. search()
C. includes()
D. match()

Q4: What does "banana".includes("na") return?

A. true
B. false
C. "na"
D. undefined

Q5: Which method is case-sensitive?

A. indexOf()
B. includes()
C. search()
D. All of the above

Q6: What is the return value of "apple".startsWith("a")?

A. "true"
B. true
C. false
D. "a"

Q7: What will "welcome".endsWith("me") return?

A. true
B. false
C. "me"
D. undefined

Q8: Which method allows using regular expressions to search in strings?

A. includes()
B. search()
C. indexOf()
D. endsWith()

Q9: What does "test".indexOf("x") return?

A. 0
B. 1
C. -1
D. true

Q10: What does "JavaScript".search("Script") return?

A. true
B. false
C. 4
D. undefined

Go Back Top