-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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.
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 |
"apple".indexOf("p"); // 1
"apple".lastIndexOf("p"); // 2
"apple".search("l"); // 3
"apple".includes("ap"); // true
"apple".startsWith("a"); // true
"apple".endsWith("e"); // true
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"
?
Q1: What is the output of "hello".indexOf("l")?
Q2: What does "hello".lastIndexOf("l") return?
Q3: Which method checks if a string contains a given value?
Q4: What does "banana".includes("na") return?
Q5: Which method is case-sensitive?
Q6: What is the return value of "apple".startsWith("a")?
Q7: What will "welcome".endsWith("me") return?
Q8: Which method allows using regular expressions to search in strings?
Q9: What does "test".indexOf("x") return?
Q10: What does "JavaScript".search("Script") return?