CoffeeScript - Data types & Variables
CoffeeScript Data types
One of the most fundamental characteristics of a programming language is the set of data types it supports. These are the type of values that can be represented and manipulated in a programming language.
As CoffeeScript compiles line by line to JavaScript, the data types provided by CoffeeScript are same as JavaScript. Except that CoffeeScript adds some additional essence.
CoffeeScript provides the following data types to work with −
Strings− The String data type represents a group of characters, in general and we represent a string value with in-between double quotes(" ")
Example: "Raj", "Rahman"
Number− The number data type represents the numerical values.
Example: 12, 212etc.
Boolean− boolean data type represents one bit of information. There are only two possible values: true and false.
CoffeeScript also defines two trivial data types, null and undefined, each of which defines only a single value. In addition to these data types, JavaScript supports two composite data types known as arrays and objects. We will cover those in detail in separate chapters.
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.
In JavaScript before using a variable we need to declare it, 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 in just by assigning a value to a literal as shown below.
name = variable name
Example
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 name variable_example.coffee
name = "Javed" age = 25
Compiling the code
Compile the above CoffeeScript code by executing the following command in the Node.js command prompt.
>compile -c variable_example.coffee
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.
(function() {
var age, name;
name = "Javed";
age = 25;
}).call(this);
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);
document.write("<br>");
test();
function test() {
i = 20;
document.write("The value of local variable i is "+i);
document.write("<br>");
}
document.write("The value of global variable i is "+i);
</script>
On executing, the above JavaScript gives you the following output
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 transpiles 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 is 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;
}).call(this);
Any way if we want we can declare a variable with global name space 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.