CoffeeScript - Variables


Variables are nothing but named containers. You can place data into these containers and then refer to the data using the name of its container.

CoffeeScript Variables

In JavaScript, before using a variable, we need to declare and initialize it (assign value). Unlike JavaScript, while creating a variable in CoffeeScript, there is no need to declare it using the var keyword. We simply create a variable just by assigning a value to a literal as shown below.

name = variable name


In the following CoffeeScript code, we have defined two variables name and age, of string and number data types respectively. Save it in a file with the name

name = "Javed"
age = 25

Compiling the code

Compile the above CoffeeScript code by executing the following command in the command prompt.

c:\> compile -c

On compiling, a JavaScript file named variable_example.js will be generated with the following content. Here you can observe that the compiler declared the variables (age and name) using the var keyword on behalf of us.

// Generated by CoffeeScript 1.10.0
(function() {
  var age, name;
  name = "Javed";
  age = 25;

Variable Scope

The scope of a variable is the region of your program in which it is defined. JavaScript and CoffeeScript variables have only two scopes.

  • Global Variables − A global variable has global scope which means it can be used anywhere in your JavaScript code.

  • Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.

The Problem with Variables in JavaScript

In JavaScript, whenever we define a variable without using the var keyword, it is created with global scope. This causes a lot of problems. Consider the following example −

<script type = "text/javascript">
   var i = 10;
   document.write("The value of global variable i is "+ i);   
   function test() {
      i = 20;
      document.write("The value of local variable i is "+i);
   document.write("The value of global variable i is "+i);

On executing, the above JavaScript gives you the following output −

The value of global variable i is 10

The value of local variable i is 20

The value of global variable i is 20

In the above example, we have created a variable named i in the global space and assigned the value 10 to it. And within the function, on an attempt to create a local variable with the same name, we have declared as i=20; without var keyword. Since we missed the var keyword, the value of global variable i is reassigned to 20.

For this reason, it is recommended to declare variables using the var keyword.

Variable Scope in CoffeeScript

Whenever we compile a CoffeeScript file, the CoffeeScript compiler creates an anonymous function, and within that function, it transcompiles the CoffeeScript code in to JavaScript line by line. (If we want, we can remove the top level function wrapper using the -b or --bare option of the compile command) Every variable that we create is declared using the var keyword within the anonymous function and thus, by default, every variable is local in CoffeeScript.

(function() {
  var age, name;
  name = "javed";
  age = 20;

Anyway, if we want, we can declare a variable with global namespace. We can do it explicitly as shown below.

obj = this
obj.age = 30

CoffeeScript Variable Names (Literals)

While naming your variables in CoffeeScript, keep the following rules in mind.

  • You should not use any of the CoffeeScript reserved keywords as a variable name. These keywords are mentioned in the next section. For example, break or Boolean variable names are not valid.

  • CoffeeScript variable names should not start with a numeral (0-9). They must begin with a letter or an underscore character. For example, 123test is an invalid variable name but _123test is a valid one.

  • CoffeeScript variable names are case-sensitive. For example, Name and name are two different variables.