Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Which is a more efficient JavaScript code snippet?
Code 1 :
for(var num=10;num>=1;num–)
{
document.writeln(num);
}
Code 2 :
var num=10;
while(num>=1)
{
document.writeln(num);
num++;
}

Which is a more efficient JavaScript code snippet?
Code 1 :
for(var num=10;num>=1;num–)
{
document.writeln(num);
}
Code 2 :
var num=10;
while(num>=1)
{
document.writeln(num);
num++;
}
a) Code 1
b) Code 2
c) Both Code 1 and Code 2
d) Cannot Compare

Answer: a
Explanation: Code 1 would be more efficient. Infact second code will go into runtime error as the value of num will never reach less than or equal to one.

Join The Discussion