Many jQuery methods implicitly iterate over all matched elements. Sometimes, however, you need to run custom logic per element — access its index, read a specific attribute, or make a decision based on its content. The .each() method provides an explicit loop over a jQuery collection.
The Callback Signature
The callback receives two arguments: the zero-based index of the current element and the raw DOM node. Inside the callback, this refers to the DOM node; wrap it in $(this) to get the jQuery object and access jQuery methods.
Breaking Early
Return false from the callback to break out of the loop early — equivalent to break in a for loop. Return true (or anything other than false) to continue to the next iteration.
$(sel).each(fn)— iterate a jQuery collection$.each(array, fn)— iterate any array or plain object$.map(array, fn)— transform and collect resultsreturn falseinside callback — breaks the loop
$.each() is the utility version that works on plain arrays and objects, not just jQuery collections, making it a general-purpose iteration tool throughout your application code.