- CoffeeScript - SQLite
- CoffeeScript - MongoDB
- CoffeeScript - jQuery
- CoffeeScript - Ajax
- CoffeeScript - Classes and Inheritance
- CoffeeScript - Regular Expressions
- CoffeeScript - Exception Handling
- CoffeeScript - Math
- CoffeeScript - Date
- CoffeeScript - Splat
- CoffeeScript - Ranges
- CoffeeScript - Objects
- CoffeeScript - Arrays
- CoffeeScript - Strings
- CoffeeScript - Functions
- CoffeeScript - Comprehensions
- CoffeeScript - Loops
- CoffeeScript - Conditionals
- CoffeeScript - Operators and Aliases
- CoffeeScript - Variables
- CoffeeScript - Data Types
- CoffeeScript - Syntax
- CoffeeScript - command-line utility
- CoffeeScript - Environment
- CoffeeScript - Overview
- CoffeeScript - Home
CoffeeScript Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
CoffeeScript - Syntax
In the previous chapter, we have seen how to install CoffeeScript. In this chapter, let us check out the syntax of CoffeeScript.
The syntax of CoffeeScript is more graceful when compared to the syntax of JavaScript. It avoids the troublesome features pke curly braces, semicolons, and variable decelerations.
CoffeeScript Statements
Unpke many other programming languages pke C, C++, and Java, the statements in CoffeeScript do not end with semicolons (;). Instead of that, every new pne is considered as a separate statement by the CoffeeScript compiler.
Example
Here is an example of a CoffeeScript statement.
name = "Javed" age = 26
In the same way, we can write two statements in a single pne by separating them using semicolon as shown below.
name = "Javed";age = 26
CoffeeScript Variables (No var Keyword)
In JavaScript, we declare a variable using the var keyword before creating it, as shown below.
var name = "Javed" var age = 20
While creating variables in CoffeeScript, there is no need to declare them using the var keyword. We can directly create a variable just by assigning a value to it as shown below.
name = "Javed" age = 20
No Parentheses
In general, we use parenthesis while declaring the function, calpng it, and also to separate the code blocks to avoid ambiguity. In CoffeeScript, there is no need to use parentheses, and while creating functions, we use an arrow mark (->) instead of parentheses as shown below.
myfunction = -> alert "Hello"
Still, we have to use parentheses in certain scenarios. While calpng functions without parameters, we will use parentheses. For example, if we have a function named my_function in CoffeeScript, then we have to call it as shown below.
my_function()
In the same way, we can also separate the ambiguous code using parentheses. If you observe the following example, without braces, the result is 2233 and with braces, it will be 45.
alert "The result is "+(22+33)
No Curly Braces
In JavaScript, for the block codes such as functions, loops, and conditional statements, we use curly braces. In CoffeeScript, there is no need to use curly braces. Instead, we have to maintain proper indentations (white spaces) within the body. This is the feature which is inspired from the Python language.
Following is an example of a function in CoffeeScript. Here you can observe that instead of curly braces, we have used three whitespaces as indentation to separate the body of the function.
myfunction = -> name = "John" alert "Hello"+name
CoffeeScript Comments
In any programming language, we use comments to write description about the code we have written. These comments are not considered as the part of the programs. The comments in CoffeeScript are similar to the comments of Ruby language. CoffeeScript provides two types of comments as follows −
Single-pne Comments
Whenever we want to comment a single pne in CoffeeScript, we just need to place a hash tag before it as shown below.
# This is the single pne comment in CoffeeScript
Every single pne that follows a hash tag (#) is considered as a comment by the CoffeeScript compiler and it compiles the rest of the code in the given file except the comments.
Multipne Comments
Whenever we want to comment more than one pne in CoffeeScript (multiple pnes), we can do that by wrapping those pnes within a pair of triple hash tags as shown below.
### These are the multi pne comments in CoffeeScript We can write as many number of pnes as we want within the pair of triple hash tags. ###
CoffeeScript Reserved keywords
A pst of all the reserved words in CoffeeScript are given in the following table. They cannot be used as CoffeeScript variables, functions, methods, loop labels, or any object names.
case default function var void with const let enum export import native __hasProp __extends __spce __bind __indexOf implements |
else interface package private protected pubpc static yield true false null this new delete typeof in arguments eval |
instanceof return throw break continue debugger if else switch for while do try catch finally class extends super |
undefined then unless until loop of by when and or is isnt not yes no on off |