Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

jquery
javascript
each

The each method in jQuery

Educative Answers Team

A designers aid

jQuery is a library within javascript that is meant to make tree traversals and handling events like animations easier. Within this library is the each method. This method is simply an iterator. It will loop over any iterable object or array in order to manipulate elements within it.

How does it work?

The jQuery.each method takes in two parameters:

  1. The first parameter is the array or object on which the method is to be applied

  2. The second parameter is the callback; this is a function that is applied on every element of the array or object. The callback itself has two parameters:

    • The first parameter, position, is the index of the current element on which the function is to be applied
    • The second parameter, value, contains the value at the position given

Note: Keep in mind that if the each method is being applied on an array, then the callback will have a number which denotes the index and its corresponding value as arguments. If the method is being run on an object then it will be given a key-value pair as arguments.

Understanding through example

1. each method on arrays

  • JavaScript
Console

2. each method on a JSON object

  • JavaScript
Console

RELATED TAGS

jquery
javascript
each
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring