Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does while(i--) mean in javascript?

I'm using swipejs from swipejs.com, the home page slider uses this logic to highlight the active un-ordered list element and I'd like to know how this while condition works.

<ul id="bullets">
   <li></li>
    ....
</ul>

var bullets = document.getElementById('bullets').getElementsByTagName('li');

var elem = document.getElementById('mySwipe');

window.mySwipe = Swipe(elem, {

callback: function(index, element) {
    var i = bullets.length;
    while (i--) {
      bullets[i].className = ' ';
    }
    bullets[index].className = 'on';
  }

});
like image 896
Mike Avatar asked Mar 04 '14 19:03

Mike


People also ask

What means while in JavaScript?

The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.

What is the meaning of while T --> 0?

This question already has answers here: In various codes where time limit exceeded, by using while(t-- >0) instead of while(t--) code runs successfully.

Is while a function in JavaScript?

The JavaScript while statement creates a loop that executes a block as long as a condition evaluates to true . The while statement evaluates the expression before each iteration of the loop. If the expression evaluates to true , the while statement executes the statement . Otherwise, the while loop exits.

Is while loop blocking in JavaScript?

Loops can execute a block of code as long as a specified condition is true.


1 Answers

The number 0 is considered falsey in JavaScript, so the while loop will run until it hits 0. You would have to be sure that i is positive to start off with, otherwise you've got an infinite loop.

like image 61
willlma Avatar answered Oct 08 '22 19:10

willlma