Javascript Index Of

| | | | | | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

The JavaScript indexOf () method returns the position of an element in a list or of a substring in a string. The JavaScript indexOf () method returns -1 if a specified value does not appear in a list or string. Only the index value of the first occurrence of a value is returned.

Often you might want to know if the string or array contains a particular value. For example, you might have a list of donut flavors sold in your donut store. You might want to check to see if you’ve removed the unpopular triple chocolate donut from the menu.

This is where the indexOf () JavaScript method comes in handy. The indexOf () method returns the first index at which a given value can be found in an array or string. If no value is found, a special value -1 is returned.

In this tutorial, we will explore the basics of the indexOf () method in JavaScript. We’ll look at two examples to show this method in action to help you better understand the indexOf () method.

JavaScript indexOf () with Array

The Array .indexOf () built-in method searches for a value in an array. The method finds the index value of the first time a specified item is displayed in an array. If a specified element is found, the indexOf () method returns the index number of the first occurrence of the value. If the specified element is not found, indexOf () will return -1.

The indexOf () method takes upper and lower case letters into account. This means that indexOf () will return -1 if a value appears in a list with a case other than the one we specified.

Here is the syntax of indexOf () method in JavaScript:

The indexOf () method is case sensitive. Here is the syntax of the indexOf () function in JavaScript:

The item parameter is the item we are looking for in our JavaScript array . This parameter is mandatory. The start_position parameter is optional. start_position is the position in our array where indexOf () starts the search.

An example scenario

Let’s say we’re out of cinnamon. We want to check if cinnamon buns are still listed on our shop menu. If donuts are on the menu, we’ll remove them. We could use the following code to perform this action:

The indexOf method returns: 2. As you can see, Cinnamon is present in our array. So our code returned the index value of the first instance of Cinnamon in our array, which in this case is 2.

The indexOf method returns: 2. As you can see, Cinnamon is present in our array. Next, our code returned the index value of the first instance of Cinnamon in our array, which in this case is 2.

Our program should return 2 even if our array contains multiple instances of Cinnamon. This is because indexOf () only returns the index position of the first instance of an element.

Let’s say our chef invented a new Jam Supreme donut and added it to our range of donut_flavors. However, she doesn’t know if it’s called the donut. Jam or Jam Supreme. He wants to check that the menu is correct.

Because Jam is already the first in the array, indexOf () will return the position of Jam. However, if we use the start_position parameter, we can skip the first position and run Here the code our boss could use to check if Jam appears after index position 1 in the array:

Our code returns: -1. While Jam exists in our array, it comes before the index value 1. The Jam does not exist ns the region we are looking for o.

JavaScrip t indexOf () with strings

indexOf () can be used to find a substring within a string . The string indexOf () method returns the index number at which a substring begins in another string.

Updating strings

Like arrays, strings in JavaScript also have their own index values. Strings are indexed left to right, starting with the index value . So for our example string above, the following index values ‚Äã‚Äãwould be assigned:

E x a m p l and < / td> !
1 2 3 4 5 6 6

So if we wanted to get the character at position d ’index 4, we could do that using the following code:

Our code returns: p.

String.indexOf ()

The String.indexOf () method can be used to check if a string contains a particular substring. If the substring can be found within the string, indexOf () will return the index position of the occurrence of the letter or string ; if the substring is not found, indexOf () will return -1.

The String.indexOf () method uses the same syntax as the Array.indexOf () method:

We need to specify an substring argument. This is the value we want to look for in our chain. The start_position argument is optional and instructs the program to start the search at a specific index. The default start_position argument is index 0.

Let’s say we have a string with the names of the donuts and we want to check if the string contains Blueberry. We could use the following code to perform this action:

Our code returns: 4. As you can see, our string donut_flavors contains Blueberry and the substring starts at the index position 4.

Our code returns: 4. As you can see, our donut_flavors string contains Blueberry and the substring begins at index position 4.

But what if we wanted to check if our string donut_flavors contained Blueberry after a specific index position? This is where the start_position argument comes in.

Let’s find the term Blueberry in our list. We will start our search from index position 10 in our list:

Our code returns: -1. While "donut_flavors" includes "Blueberry," the string begins before the index value 10 in the array. Our code returns "-1". This indicates that the substring was not found.

Check if a value exists using indexOf ()

You can use an "if" statement with the indexOf () method to check if a value exists in a string or an array. For example, you can use indexOf () to check if the donut menu contains a particular value. Then you can print a message to the console if this value is found.

Let’s write a program that prints a message to the console if "Chocolate" is in a donut list. Here is our code:

Our code returns: "Chocolate donuts are on the menu! ".

On the first line of our code, we declare our donut flavor array called "donut_flavors," which contains six values.

Next, we create an if statement that uses indexOf to check if "Donut_flavors" contains "Chocolate." This if statement evaluates whether indexOf returns a value equal to "-1."

If indexOf returns "-1," means that our value was not found in the array. This means that our if statement will not be executed. If indexOf returns another value, our if statement will be executed.

In this case, indexOf () will return 2. This is because "Chocolate" it is present in our array "donut_flavors ". Our program displays "Chocolate donuts are on the menu !" because indexOf returned a value that is not equal to -1.

Conclusion

The JavaScript method indexOf () checks if a string or array contains a particular value. The method returns the index number at which this value is found in the string or array. If the specified value is not found, indexOf () returns "-1."

In this tutorial, we covered the basics of arrays, strings and how they are indexed, so we explored how to use the indexOf () method with strings and arrays to check if it contained a specific value.

We have explained how to use the indexOf () and an "if" statement to check if a value exists in a list or a string.

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Javascript Index Of, check other array Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Ken Innsbruck

Prague | 2023-03-23

I was preparing for my coding interview, thanks for clarifying this - Javascript Index Of in Python is not the simplest one. Checked yesterday, it works!

Manuel Jackson

Paris | 2023-03-23

Thanks for explaining! I was stuck with Javascript Index Of for some hours, finally got it done 🤗. Will use it in my bachelor thesis

Javier Sikorski

Rome | 2023-03-23

SPL PHP module is always a bit confusing 😭 Javascript Index Of is not the only problem I encountered. I just hope that will not emerge anymore

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically