Home » JavaScript » JavaScript Statements » Question

JavaScript Statements

  1. Which is a more efficient code snippet?
    Code 1 :
    for(var n=10;n>=1;n--)
    {
    document.writeln(n);
    }


    Code 2 :

    var n=10;
    while(n>=1)
    {
    document.writeln(n);
    n++;
    }
    1. Code 2
    2. Code 1
    3. Cannot Compare
    4. Both Code 1 and Code 2
    5. None of these
Correct Option: B

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.



Your comments will be displayed only after manual approval.