---
title: "What is the purpose of the .each() function in jQuery? Explain with an example."  
description: "What is the purpose of the .each() function in jQuery? Explain with an example."  
author: "Revati S Misra"  
published: 2023-04-14  
updated: 2023-06-12  
canonical: https://www.mindstick.com/forum/157818/what-is-the-purpose-of-the-each-function-in-jquery-explain-with-an-example  
category: "jquery"  
tags: ["jquery", "javascript", "jquery selectors"]  
reading_time: 2 minutes  

---

# What is the purpose of the .each() function in jQuery? Explain with an example.

What is the [purpose](https://yourviews.mindstick.com/view/247/no-fail-policy-failing-its-purpose) of the .each() [function in jQuery](https://www.mindstick.com/forum/33894/how-to-use-load-function-in-jquery)? [Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) with an example.

## Replies

### Reply by Aryan Kumar

The jQuery .each() [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) is used to iterate over a jQuery object, executing a function for each matched element. The function is passed two arguments: the current element and its index.

Here is an example of how to use the .each() function to change the color of all the even-numbered li elements in a list:

Code snippet

```plaintext
$("li").each(function(index) {
  if (index % 2 === 0) {
    $(this).css("color", "red");
  }
});
```

This code will iterate over all the li elements in the document and call the function for each one. The function will check the index of the current element. If the index is even, the function will change the color of the element to red.

The .each() function is a powerful tool that can be used to perform a variety of tasks on a jQuery object. It is a versatile function that can be used in a variety of situations.

Here are some other examples of how to use the .each() function:

- To add a class to all the even-numbered li elements in a list:

Code snippet

```plaintext
$("li").each(function(index) {
  if (index % 2 === 0) {
    $(this).addClass("even");
  }
});
```

- To hide all the li elements that contain the word "hidden" in their text:

Code snippet

```plaintext
$("li").each(function() {
  if ($(this).text().indexOf("hidden") !== -1) {
    $(this).hide();
  }
});
```

- To loop through an array of numbers and print each number to the console:

Code snippet

```plaintext
var numbers = [1, 2, 3, 4, 5];

numbers.each(function(index, number) {
  console.log(number);
});
```

The .each() function is a powerful tool that can be used to perform a variety of tasks on a jQuery object, an array, or an object. It is a versatile function that can be used in a variety of situations.


---

Original Source: https://www.mindstick.com/forum/157818/what-is-the-purpose-of-the-each-function-in-jquery-explain-with-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
