Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Ranges

Ranges in CoffeeScript enable us to easily populate an array with numbers ranging from a start number to an end number. The syntax for building a range looks like so:

Example: (source: range1.coffee)


myRange = [1..10]
console.log myRange


Example: (source: range1.js)


(function() {
  var myRange;

  myRange = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

  console.log(myRange);

}).call(this);


Output: (source: range1.coffee)


[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]


By separating the start number and the end number with .. we will get an array filled with numbers, starting with the first number and going up to and including the end number. If we want to exclude the end number, we would use ... instead of ..:

Example: (source: range2.coffee)


myRange = [1...10]
console.log myRange


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial