The new for loop in jQuery 3

Love the fact that the for loops are now iterations in jQuery. The old way of doing loops like this:

var $elements = $(".myclass");
for (var x=0; x< $elements.length; x++){
       $elements[x].toggleClass("shadow);
}

Can now look like this in jQuery 3:

var $elements = $(".myclass");
for (var element of $elements){
       element.toggleClass("shadow);
}
Advertisements

9 thoughts on “The new for loop in jQuery 3

Add yours

  1. I’d prob stick with an Each loop as being less code. Wonder the rationale behind this syntax – first glance looks like a strange hybrid of For and Each loop – although don’t really see the value, unless planning to phase out Each!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: