Coffee Script Syntax - CoffeeScript

What is Coffee Script Syntax?

In the previous chapter, we've seen a way to set up Coffee Script. On this chapter, let us take a look at out the syntax of Coffee Script.
The syntax of Coffee Script is extra graceful whilst compared to the syntax of JavaScript. It avoids the troublesome capabilities like curly braces, semicolons, and variable decelerations.
Coffee Script Statements

not like many other programming languages like C, C++, and Java, the statements in Coffee Script do now not end with semicolons (;). in place of that, each new line is taken into consideration as a separate announcement via the Coffee Script compiler.

Instance

Right here is an example of a Coffee Script announcement.

In the same way, we can write two statements in a single line by separating them using semicolon as shown below.

CoffeeScript Variables (No var Keyword)

In JavaScript, we declare a variable using the var keyword before creating it, as shown below.

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.

No Parentheses

In popular, we use parenthesis at the same time as declaring the function, calling it, and also to split the code blocks to avoid ambiguity. In CoffeeScript, there's no need to apply parentheses, and at the same time as creating functions, we use an arrow mark (->) in preference to parentheses as proven underneath.

Still, we have to use parentheses in certain eventualities. Whilst calling functions without parameters, we can use parentheses. For example, if we've got a function named my_function in Coffee Script, then we've to name it as proven under.

Within the equal manner, we also can separate the ambiguous code the use of parentheses. in case you study the following example, without braces, the result is 2233 and with braces, it is going to be 45.

No Curly Braces

In JavaScript, for the block codes which includes capabilities, loops, and conditional statements, we use curly braces. In CoffeeScript, there is no need to apply curly braces. as a substitute, we ought to keep proper indentations (white spaces) inside the frame. that is the feature that's stimulated from the Python language.

Following is an instance of a characteristic in CoffeeScript. here you may look at that instead of curly braces, we've got used three whitespaces as indentation to separate the frame of the function.

What are the Coffee Script comments?

In any programming language, we use comments to write a description about the code we've written. These remarks are not considered as the part of the applications. The remarks in Coffee Script are much like the comments of Ruby language. Coffee Script gives two sorts of comments as follows −

What are single-line comments?

On every occasion we want to remark a single line in Coffee Script, we just need to place a hash tag before it as shown underneath.

Each single line that follows a hash tag (#) is taken into consideration as a comment by using the CoffeeScript compiler and it compiles the rest of the code inside the given report except the comments.

What are Multiline comments?

Every time we want to comment more than one line in Coffee Script (a couple of lines), we are able to do this by means of wrapping those strains within a couple of triple hash tags as proven below.

Coffee Script Reserved keywords

A list of all the reserved words in Coffee Script is given in the following desk. They can't be used as Coffee Script variables, functions, techniques, loop labels, or any item names.

case

default

function

var

void

with

const

let

enum

export

import

native

__hasProp

__extends

__slice

__bind

__indexOf

implements

else

interface

package

private

protected

public

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

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

CoffeeScript Topics