পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Tuesday, February 14, 2017

CoffeeScript - Arrays

The Array object lets you store multiple values in a single variable. It stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Syntax

To create an array, we have to instantiate it using the new operator as shown below.
array = new (element1, element2,....elementN)
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.
array = [element1, element2, ......elementN]

Example

Following is an example of defining an array in CoffeeScript. Save this code in a file with name array_example.coffee
student = ["Rahman","Ramu","Ravi","Robert"]

Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c array_example.coffee
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
  var student;

  student = ["Rahman", "Ramu", "Ravi", "Robert"];

}).call(this);

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.
student = [
  "Rahman"
  "Ramu"
  "Ravi"
  "Robert"
  ]

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
students = [ "Rahman", "Ramu", "Ravi", "Robert" ]
console.log student for student in students 
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c array_comprehensions.coffee
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
  var i, len, student, students;

  students = ["Rahman", "Ramu", "Ravi", "Robert"];

  for (i = 0, len = students.length; i  len; i++) {
    student = students[i];
    console.log(student);
  }

}).call(this);
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee array_comprehensions.coffee
On executing, the CoffeeScript file produces the following output.
Rahman
Ramu
Ravi
Robert
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.
students = [ "Rahman", "Ramu", "Ravi", "Robert",21 ]

No comments:

Post a Comment