What is indexof in JavaScript and How does it works?

Are you ready to discover the functionality of the indexOf() method with both strings and arrays in JavaScript?

In this article, you will learn how it locates the position of a specific value within a string or an array and understand its syntax, parameters, and return values through practical examples.

Dive into the nuances of JavaScript zero-based indexing and explore how indexOf() can be used in various coding scenarios.

What is indexof?

The indexOf() method in JavaScript is a handy tool that is used to find out where a specific piece of data is located within a string or an array.

What is JavaScript String indexOf() Method?

The string.indexOf() method in JavaScript is used with strings to find the position of the first occurrence of a specified value.

This method returns the position of the first instance of a certain value within that string. If the value isn’t found in the string, it returns -1.

It’s important to remember that this function is case sensitive, so “Itsourcecode” and “itsourcecode” would be considered different values.

For example:

let sampletext = "Hi, Welcome to Itsourcecode!";
let result = sampletext.indexOf("itsourcecode");✅  
console.log(result)

Output:

-1

Syntax

string.indexOf(searchValue[, fromIndex])

Parameters

searchValue (required)

The string value that you want to search for in the string.

fromIndex (optional)

The position in the string where you want to start the search. The default value is 0.

Return value

If the searchValue is found, indexOf() returns the position of the first occurrence of the specified value.
If the searchValue is not found, it returns -1.

Here’s an example:

let sampletext = "Hi, Welcome to Itsourcecode!";
let result = sampletext.indexOf("Itsourcecode"); ✅ 
console.log(result)

Output:

15

As you can see, the word “Itsourcecode” starts at position 15 in the string.

The spaces and punctuation marks are also counted as characters. Therefore, the output of sampletext.indexOf(“Itsourcecode”) is correctly 15.

Here’s another example:

let sampletext = "Itsourcecode give you free sourcecodes and tutorials.";
let position = sampletext.indexOf("sourcecodes"); ✅

if (position !== -1) {
  console.log(`The word "sourcecodes" was found at position ${position} in the text.`);
} else {
  console.log('The word "sourcecodes" was not found in the sentence.');
}

Output:

The word "sourcecodes" was found at position 27 in the text.

You can also use indexOf() with arrays, so keep on reading to understand it thoroughly. 😉

What is JavaScript Array indexOf() Method?

The array.indexOf() method in JavaScript is a built-in function that’s used with arrays.

It returns the first index at which a given element can be found in the array. If the element isn’t in the array, it will return -1.

Syntax

array.indexOf(element, start) ✅

Parameters

element (required)

The element to locate in the array.

start (optional)

The index to start the search at. If omitted, the search starts at index 0.

Return value

The array.indexOf method in JavaScript returns the first index of the element in the array; -1 if not found.

Here’s an example:

let subjects = ["Programming", "Web Development", "Software Development", "Software Engineering"];
let result = subjects.indexOf("Software Development");✅  
console.log(result)

In this example, the subject “Software Development” is found at position 2 in the array.

If the subject “Software Development” was not found in the array, the result would be -1.

Output:

2

Here’s another example with an array.indexof in JavaScript:

const numbers = [100, 200, 300, 400, 500];
const result = numbers.indexOf(500); ✅
console.log(result)

Output:

4

📌Please take note that both these methods start their search from index 0.

Supported Browser

✔ Chrome

✔ Edge

✔ Firefox

✔ Internet Explorer

✔ Safari

✔ Opera

What does indexOf == 0 mean in JavaScript?

The indexOf() == 0 In JavaScript is a condition that checks if the position of a specified value is at the start of a string or an array.

For instance, if you have a string and you want to check if it starts with a certain substring, you can use indexOf() == 0.

Here’s an example:

let samplestr = "Welcome to Itsourcecode!";
if (samplestr.indexOf("Welcome") == 0) {
console.log("The string starts with 'Welcome'");
} else {
console.log("The string does not start with 'Welcome'");
}

In this case, samplestr.indexOf(“Welcome“) returns 0 because “Welcome” is at the start o in the beginning of the string.

So, samplestr.indexOf(“Welcome“) == 0 is true.

Output

The string starts with 'Welcome'

Similarly, for an array.indexOf() == 0 checks if a certain element is the first element in the array in JavaScript.

If it is, indexOf() returns 0, and indexOf() == 0 is true.

Here’s an example:

let subjects = ["Programming", "Web Development", "Software Development", "Software Engineering"];
if (subjects.indexOf("Programming") == 0) ✅  {
  console.log("The array starts with 'Programming'");
} else {
  console.log("The array does not start with 'Programming'");
}

Output:

The array starts with 'Programming'

📌Remember that JavaScript uses zero-based indexing, so the first position in a string or an array is 0.

What is the difference between the search () and indexOf () method in JavaScript?

The search() and indexOf() methods in JavaScript are both used to find the position of a substring in a string, but they have some key differences:

Regular expression support

The search() method in JavaScript has the ability to execute a search using a regular expression, a feature that the indexOf() method lacks.

Starting position argument

The indexOf() method has the unique capability to accept a second argument that determines the starting point of the search within the string.

This is a functionality that the search() method does not offer.

Here’s an example:

let samplestr = "Itsourcecode!";
console.log(samplestr.indexOf("o")); ✅ // Outputs: 3
console.log(samplestr.search("o")); // Outputs:3 

console.log(samplestr.indexOf("o", 5)); // Outputs: 9 (starts searching from index 5)
// search() doesn't support a starting position argument

console.log(samplestr.search(/o/i));   // Outputs: 3 (supports regular expressions)
// indexOf() doesn't support regular expressions

In this code, both indexOf(“o”) and search(“o”) return 3, because “o” is found at position 3 in the string.

However, indexOf(“o”, 5) returns 9, because it starts searching from index 5.

The search(/o/i) returns 3 because it supports regular expressions (in this case, a case-insensitive search for “o”).

So, if you need to use a regular expression or if performance is a concern, use the search() method.

If you want to designate a specific starting point for your search, the indexOf() method is the way to go.

Conclusion

In conclusion, the indexOf() method in JavaScript is a versatile tool used to locate the position of a specific value with both strings and arrays.

It’s important to remember that JavaScript uses zero-based indexing, so the first position in a string or an array is 0.

The indexOf() method returns -1 if the specified value is not found. 

For strings, its case sensitive, meaning “Itsourcecode” and “itsourcecode” are considered different values.

For arrays, it returns the first index at which a given element can be found.

We are hoping that this article provides you with enough information that helps you understand the indexof in JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Leave a Comment