Book Image

CoffeeScript Application Development Cookbook

By : Mike Hatfield
Book Image

CoffeeScript Application Development Cookbook

By: Mike Hatfield

Overview of this book

Table of Contents (18 chapters)
CoffeeScript Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with strings


In this section, we will look at the various aspects of working with strings or text-based data.

String interpolation

In this section, we will demonstrate the CoffeeScript feature of string interpolation.

Getting ready

In JavaScript, creating strings that include variable values involves concatenating the various pieces together. Consider the following example:

var lineCount = countLinesInFile('application.log');
var message = "The file has a total of " + lineCount + " lines";
console.log(message);

This can get pretty messy and CoffeeScript provides an elegant solution to avoid this called string interpolation.

How to do it...

CoffeeScript provides the ability to perform string interpolation by using double quoted strings containing one or more #{} delimiters.

The preceding example can be written as follows:

lineCount = countLinesInFile 'application.log'
message = "The file has a total of #{lineCount} lines"
console.log message

This not only requires less typing, but it can also...