CoffeeScript Loops - CoffeeScript

What is CoffeeScript Loops?

While coding, you could encounter a situation wherein you need to execute a block of code over and over again. In such situations, you may use loop statements.

In general, statements are achieved sequentially: the primary declaration in a function is executed first, accompanied by the second one, and so on.

A loop statement permits us to execute a statement or group of statements a couple of times. Given underneath is the overall shape of a looped announcement in most of the programming languages


JavaScript offers while, for and loops. The loops in CoffeeScript are just like those in JavaScript.

at the same time as a loop and its variants are the simplest loop constructs in CoffeeScript. as opposed to the usually used for loop, CoffeeScript gives you Comprehensions which are mentioned in the element in later chapters.

The while loop in CoffeeScript

The while loop is the most effective low-stage loop that CoffeeScript presents. It incorporates a Boolean expression and a block of statements. The while loop executes the required block of statements repeatedly as long as the given Boolean expression is true. once the expression will become fake, the loop terminates.


Following is the syntax of the while loop in CoffeeScript. Right here, there's no need of the parenthesis to specify the Boolean expression and we need to indent the body of the loop the usage of (consistent range of) whitespaces rather than wrapping it with curly braces.


The following example demonstrates the usage of while loop in CoffeeScript. Save this code in a file with name

Open the command prompt and compile the .coffee file as shown below.

Now, open the command prompt again and run the CoffeeScript file as shown below.

On executing, the CoffeeScript file produces the following output.

Set the variable to different value and then try

Variants of while

The While loop in CoffeeScript have two variants namely the until variant and the loop variant.


Loop Type & Description


until variant of while

Theuntilvariant of thewhileloop contains a Boolean expression and a block of code. The code block of this loop is executed as long as the given Boolean expression is false.


loop variant of while

Theloopvariant is equivalent to thewhileloop with true value(while true). The statements in this loop will be executed repeatedly until we exit the loop using theBreakstatement.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

CoffeeScript Topics