Language concepts

Loop invariant

Try the free JavaScript compiler

A property chosen to hold before the first iteration and be preserved by every completed iteration at the chosen point. Together with exit conditions it helps prove correctness.

Formal Syntax & Structure

javascript syntaxjavascript
// At loop head: processed + remaining === initial

Working Example

Example Snippetjavascript
let a=3,b=0; while(a>0){a--;b++;} console.log(a+b);
JavaScript Reference