Searching: includes, indexOf & startsWith in JavaScript
Three search tiers: includes (boolean presence — the modern default), indexOf/lastIndexOf (positions, -1 when absent), and startsWith/endsWith (boundary checks). includes reads as intent (if (url.includes("https"))), indexOf serves slicing and pre-regex code, and the boundary pair validates prefixes/suffixes directly.
Case sensitivity trips everyone once — normalize both sides (toLowerCase) before comparing, or reach for regex with the i flag. For presence questions, includes beats indexOf !== -1 on readability every time.
includes for presence
"hello".includes("ell") is true — boolean answers without index bookkeeping.
indexOf for positions
Returns the first index or -1 — compare !== -1 explicitly, since index 0 is falsy.
Boundaries directly
startsWith("https") and endsWith(".png") check edges — URL and file validation staples.
Normalize case first
Lowercase both sides before comparing, or use case-insensitive regex — raw mixed-case search misses.
let url = "https://example.com/a.png";console.log(url.includes("https"));console.log(url.indexOf("z"));console.log(url.endsWith(".png"));Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.