Here, we use the next() method both as an iterator (since it returns true whenever it's successful) in the condition of the while loop:
var gr = new GlideRecord('incident'); gr.addQuery('active', 'true'); gr.query(); while (gr.next()) { //do something }
Don't get confused between the return value of the next(), and hasNext() methods. next() will return true if it found a next record in the list matching your query filter, and has loaded it into the GlideRecord object. On the other hand, hasNext() will return true if there exists a next record in the database, but it won't load it up. The former tells you about the record you're currently (trying to) iterate into, and the latter looks at the next record to see if one exists ...