L

Lalit Singh • 4.06K Points
Extraordinary

Q. Among the following given JavaScript snipped codes, which is more efficient

Code:
Code 1

for(var number=10;number>=1;number--)  
{  
document.writeln(number);  
}  

Code 2:

var number=10;  
while(number>=1)  
{  
document.writeln(number);  
       number++;  
}  
  • (A) Code 1
  • (B) Code 2
  • (C) Both Code 1 and Code 2
  • (D) Cannot Compare

Explanation by: Lalit Singh
Code 1 will be more efficient. In fact, the second code may encounter a runtime error because the value of "number" is never going to be equal to or less than one.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.