CoffeeScript Arrays - CoffeeScript

What is CoffeeScript Arrays?

The Array item lets you save multiple values in a single variable. It stores a hard and fast-length sequential collection of factors of the equal type. An array is used to keep a set of statistics, but it's far regularly more useful to think of an array as a group of variables of the identical type.

Syntax

To create an array, we have to instantiate it the usage of the new operator as proven under.

The Array() constructor accepts the list of string or integer types. We can also specify the length of the array by passing a single integer to its constructor.

We can also define an array by simply providing the list of its elements in the square braces ([ ]) as shown below.

Example

Following is an example of defining an array in CoffeeScript. Save this code in a file with name array_example.coffee

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

On compiling, it gives you the following JavaScript.

New line instead of comma

We can also remove the comma (,) between the elements of an array by creating each element in a new line by maintaining proper indentation as shown below.

Comprehensions over arrays

We can retrieve the values of an array using comprehensions.

Example

The following example demonstrates the retrieval of elements of an array using comprehensions. Save this code in a file with name array_comprehensions.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.

Unlike the Arrays in other programming languages the arrays in CoffeeScript can have multiple types of data i.e. both string and numericals.

Example

Here is an example of a CoffeeScript array holding multiple types of data.

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

CoffeeScript Topics