Home » JavaScript » JavaScript While Loop » Question

JavaScript While Loop

  1. What will the following code snippet work? If not, what will be the error?
    function test(p) 
    {
    for (; p.next; p = p.next) ;
    return p;
    }
    1. No, this will not iterate
    2. No, this will result in a runtime error with the message “Cannot use Linked List”
    3. No, this will throw an exception as only numerics can be used in a for loop
    4. Yes, this will work
    5. None of these
Correct Option: D

The above code uses a for loop to traverse a linked list data structure and return the last object in the list. This will perfectly work.



Your comments will be displayed only after manual approval.