Skip to content

JavaScript Tutorial Strings

JavaScript String Search Methods

Lesson 31 of 258 2 min read By Deepak Paswan Updated Oct 4, 2026

These methods answer questions about a string. They are case sensitive.

includes

Does the string contain this text anywhere?

script.js
JS
const title = "Learn JavaScript Today";

console.log(title.includes("JavaScript")); // → true
console.log(title.includes("javascript")); // → false (case matters)
console.log(title.includes("Python"));     // → false

startsWith and endsWith

script.js
JS
const file = "report-2026.pdf";

console.log(file.startsWith("report")); // → true
console.log(file.endsWith(".pdf"));     // → true
console.log(file.endsWith(".png"));     // → false

indexOf

indexOf returns the position where the text starts, or -1 if it is not found.

script.js
JS
const text = "banana";
console.log(text.indexOf("an"));  // → 1
console.log(text.indexOf("xyz")); // → -1
console.log(text.lastIndexOf("an")); // → 3

Use includes() when you only need yes or no. Use indexOf() when you need the position.

Ignoring case

Convert both sides first.

script.js
JS
const query = "JAVASCRIPT";
const title = "Learn JavaScript Today";

console.log(title.toLowerCase().includes(query.toLowerCase())); // → true
script.js
JS
const articles = ["JavaScript arrays", "CSS grid guide", "Intro to JavaScript"];
const term = "javascript";

const matches = articles.filter(title => title.toLowerCase().includes(term));
console.log(matches); // → ["JavaScript arrays", "Intro to JavaScript"]

Later you will connect this to a text input in the search filter project.

warning
Using the result of indexOf as a condition
if (text.indexOf("a")) is false when "a" is at position 0, and true when it is not found (-1 is truthy). Compare explicitly: text.indexOf("a") !== -1, or just use includes().