An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. Let us take a simple expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and ‘+’ is called the operator.
The operators provided by CoffeeScript are same as in JavaScript except a few differences. There are some problematic operators in JavaScript. CoffeeScript either removed them or modified their functionality and it also introduced some new operators.
Following is the list of operators supported by CoffeeScript.
In addition to operators, CoffeeScript also provides aliases. CoffeeScript provides aliases to various operators and symbols in order to make your CoffeeScript code readable and more user friendly.
Let us have a look at all the operators and aliases of CoffeeScript one by one.
CoffeeScript supports the following arithmetic operators. Assume variable A holds 10 and variable B holds 20, then −
S.No | Operator and Description | Example |
---|---|---|
1 | + (Addition) Adds two operands |
A + B = 30 |
2 | − (Subtraction) Subtracts the second operand from the first |
A - B = -10 |
3 | * (Multiplication) Multiply both operands |
A * B = 200 |
4 | / (Division) Divide the numerator by the denominator |
B / A = 2 |
5 | % (Modulus) Outputs the remainder of an integer division |
B % A = 0 |
6 | ++ (Increment) Increases an integer value by one |
A++ = 11 |
7 | -- (Decrement) Decreases an integer value by one |
A-- = 9 |
JavaScript supports the following comparison operators. Assume variable A holds 10 and variable B holds 20, then −
S.No | Operator and Description | Example |
---|---|---|
1 | = = (Equal) Checks if the value of two operands are equal or not, if yes, then the condition becomes true. |
(A == B) is not true. |
2 | != (Not Equal) Checks if the value of two operands are equal or not, if the values are not equal, then the condition becomes true. |
(A != B) is true. |
3 | > (Greater than) Checks if the value of the left operand is greater than the value of the right operand, if yes, then the condition becomes true. |
(A > B) is not true. |
4 | < (Less than) Checks if the value of the left operand is less than the value of the right operand, if yes, then the condition becomes true. |
(A < B) is true. |
5 | >= (Greater than or Equal to) Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes, then the condition becomes true. |
(A >= B) is not true. |
6 | <= (Less than or Equal to) Checks if the value of the left operand is less than or equal to the value of the right operand, if yes, then the condition becomes true. |
(A <= B) is true. |
Following table shows the aliases for few of the Comparison operators. Suppose A holds 20 and variable B holds 20.
Operator | Alias | Example |
---|---|---|
= = (Equal) | is | A is B gives you true. |
!= = (Not Equal) | isnt | A isnt B gives you false. |
CoffeeScript supports the following logical operators. Assume variable A holds 10 and variable B holds 20, then −
S.No | Operator and Description | Example |
---|---|---|
1 | && (Logical AND) If both the operands are non-zero, then the condition becomes true. |
(A && B) is true. |
2 | || (Logical OR) If any of the two operands are non-zero, then the condition becomes true. |
(A || B) is true. |
3 | ! (Logical NOT) Reverses the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false. |
! (A && B) is false. |
The following table shows the aliases for some of the logical operators. Suppose X holds true and variable Y holds false.
Operator | Alias | Example |
---|---|---|
&& (Logical AND) | and | X and Y gives you false |
|| (Logical OR) | or | X or Y gives you true |
! (not x) | not | not X gives you false |
CoffeeScript supports the following bitwise operators. Assume variable A holds 2 and variable B holds 3, then −
S.No | Operator and Description | Example |
---|---|---|
1 | & (Bitwise AND) It performs a Boolean AND operation on each bit of its integer arguments. |
(A & B) is 2. |
2 | | (BitWise OR) It performs a Boolean OR operation on each bit of its integer arguments. |
(A | B) is 3. |
3 | ^ (Bitwise XOR) It performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both. |
(A ^ B) is 1. |
4 | ~ (Bitwise Not) It is a unary operator and operates by reversing all the bits in the operand. |
(~B) is -4. |
5 | << (Left Shift) It moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on. |
(A << 1) is 4. |
6 | >> (Right Shift) Binary Right Shift Operator. The left operand’s value is moved right by the number of bits specified by the right operand. |
(A >> 1) is 1. |
CoffeeScript supports the following assignment operators −
S.No | Operator and Description | Example |
---|---|---|
1 | = (Simple Assignment ) Assigns values from the right side operand to the left side operand |
C = A + B will assign the value of A + B into C |
2 | += (Add and Assignment) It adds the right operand to the left operand and assigns the result to the left operand. |
C += A is equivalent to C = C + A |
3 | -= (Subtract and Assignment) It subtracts the right operand from the left operand and assigns the result to the left operand. |
C -= A is equivalent to C = C - A |
4 | *= (Multiply and Assignment) It multiplies the right operand with the left operand and assigns the result to the left operand. |
C *= A is equivalent to C = C * A |
5 | /= (Divide and Assignment) It divides the left operand with the right operand and assigns the result to the left operand. |
C /= A is equivalent to C = C / A |
6 | %= (Modules and Assignment) It takes modulus using two operands and assigns the result to the left operand. |
C %= A is equivalent to C = C % A |
Note − Same logic applies to Bitwise operators so they will become like <<=, >>=, >>=, &=, |= and ^=.
While working with JavaScript, you will encounter two types of equality operators == and ===.
The == operator in JavaScript is type coercive, i.e., if the types of the two operands in an operation are different, then the data type of one of the operator is converted into other and then both are compared.
CoffeeScript avoids this undesirable coercion, it compiles the == operator in to the strict comparison operator of JavaScript ===.
If we compare two operands using ===, then it returns true, only if both the value and datatypes of them are equal, else it returns false.
Consider the following example. Here we have two variables a and b. a holds the value 21 of integer type and b holds the same value, but it is of string type. In CoffeeScript, when we compare a and b, the result will be false. (Since the == operator of CoffeeScript is converted to === operator of JavaScript)
a=21 b="21" result = 21=='21' console.log result
On compiling, the above CoffeeScript produces the following JavaScript
// Generated by CoffeeScript 1.10.0 (function() { var a, b, result; a = 21; b = "21"; result = a === b; console.log(result); }).call(this);
On executing, it produces the following output.
false
CoffeeScript provides a new operator known as existential operator to verify the existence of a variable. It is denoted by ?. Unless a variable is null or undefined, the existential operator returns true.
Given below is an example of the existential operator. Here we have three variables, namely name, age, and subject and we are verifying the existence of the variables name and phone using existential operator.
name="Ramu" age=24 subject="Engineering" verify_name = name? verify_phone = phone? console.log verify_name console.log verify_phone
On compiling, this will generate the following JavaScript code.
// Generated by CoffeeScript 1.10.0 (function() { var age, name, subject, verify_name, verify_phone; name = "Ramu"; age = 24; subject = "Engineering"; verify_name = name != null; verify_phone = typeof phone !== "undefined" && phone !== null; console.log(verify_name); console.log(verify_phone); }).call(this);
If we execute the above CoffeeScript file, it produces the following output.
true false
Note − We have an accessor variant of the existential operator ?. We can use it instead of the . operator to find out the null references.
As in Python, we can use a chain of comparison operators in a single expression in CoffeeScript.
Following is an example of using chained comparison.
score = 70 passed = 100 > score > 40 console.log passed
On compiling, the example CoffeeScript gives you the following JavaScript code.
// Generated by CoffeeScript 1.10.0 (function() { var passed, score; score = 70; passed = (100 > score && score > 40); console.log(passed); }).call(this);
If you execute the above CoffeeScript code, it produces the following output.
true
Note − CoffeeScript removes the ternary operator; instead of it, we can use the inline if statement.
In general, CoffeeScript provides aliases to various operators and symbols in order to make your CoffeeScript code readable and more user friendly. Following are the aliases provided by CoffeeScript.
Name | Operator / symbol | Aliases |
---|---|---|
"equals to" operator | == | is |
"not equals to" operator | !== | isnt |
"not" operator | ! | not |
"and" operator | && | and |
"or" operator | || | or |
boolean value true | true | true, yes, on |
boolean value false | false | off, no |
current object | this | @ |
new line (or) semi colon | \n or ; | then |
Inverse of if | ! if | unless |
To test for array presence | in | |
To test for object presence | of | |
Exponentiation | a**b | |
Integer division | a//b | |
dividend dependent modulo | a%%b |
The following example shows how to use aliases in CoffeeScript −
a=21; b=21 x = true; y = false console.log a is b console.log a isnt b console.log x and y console.log x or y console.log yes or no console.log on or off console.log a**b console.log a//b console.log a%%b
On compiling the above example, it gives you the following JavaScript code.
// Generated by CoffeeScript 1.10.0 (function() { var a, b, x, y, modulo = function(a, b) { return (+a % (b = +b) + b) % b; }; a = 21; b = 21; x = true; y = false; console.log(a === b); console.log(a !== b); console.log(x && y); console.log(x || y); console.log(true || false); console.log(true || false); console.log(Math.pow(a, b)); console.log(Math.floor(a / b)); console.log(modulo(a, b)); }).call(this);
If you execute the above CoffeeScript file, it produces the following output −
true false false true true true 5.842587018385982e+27 1 0