These methods answer questions about a string. They are case sensitive.
includes
Does the string contain this text anywhere?
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
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.
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.
const query = "JAVASCRIPT";
const title = "Learn JavaScript Today";
console.log(title.toLowerCase().includes(query.toLowerCase())); // → true
A real example: a simple search
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.
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().