Skip navigation

Consider the below code

//
aAge = Array(31,23,42,44,25);  //1
aLength = aAge.length;  //2
for(i=0;i<aLength;i++)   //3
  alert(aAge[i]);   //3
//

How to avoid line number 2?

//
aAge = Array(31,23,42,44,25);  //1
for(i=0;i<aAge.length;i++)   //2
  alert(aAge[i]);   //3
//

But to avoid length calculation each iteration

//
aAge = Array(31,23,42,44,25);  //1
for(i=0,aLength=aAge.length;i< aLength;i++)   //2
  alert(aAge[i]);   //3
//

A cool trick!! May be useful to you guys!!!

Advertisements

One Trackback/Pingback

  1. By For loop trick – part 2 « Rajakvk’s Blog on 07 Nov 2011 at 12:03 pm

    […] is a expressive language. In continuation to the previous post, check the following […]

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: