CoffeeScript - Comprehensions -CoffeeScript - for..of comprehensions - CoffeeScript

Just like Arrays, CoffeeScript provides containers to store key-value pairs known as objects. We can iterate objects using the for..of comprehensions provided by CoffeeScript.

Syntax

Suppose we have an object in CoffeeScript as { key1: value, key2: value, key3: value} then you can iterate these elements using the for..of comprehension as shown below.

Example

The following example demonstrates the usage of the for..of comprehension provided by CoffeeScript. Save this code in a file with name for_of_example.coffee

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

On compiling, it gives you the following JavaScript.

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

On executing, the CoffeeScript file produces the following output.

Note − We will discuss arrays, objects, and ranges in detail in individual chapters later in this tutorial.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

CoffeeScript Topics