JavaScript: Part – 2


This blog introduces the grammar of the good parts of JavaScript, presenting a good overview of how the language is structured.

The grammar fo the good parts presented in this blog is significantly simpler that the grammar of the whole language.


Whitespace can take the form of formatting character or comments. Whitespace is usually insignificant but it is necessary to use Whitespace to avoid combining them into a single token.


var that = this;

The space between var and that cannot be removed, but the other spaces can be removed.

JavaScript offers two form of comments, block comment formed with /* */ and the line ending comments starting with //.

/*      This is a comment      */

This is a comment //

Comments should be used liberally to improve the readability of your programs. Take care that the comments always accurately describe the code.

Obsolete comments are worse that no comments.


A name is a letter optionally followed by one or more letters, digits, or underbars. A name cannot be one of these reserved words.

  • abstract
  • Boolean break byte
  • case catch char const continue
  • debugger default delete do double
  • else enum export extends
  • false final finally float for function
  • goto
  • if implements import in instanceof int interface
  • long
  • native new null
  • package private protected public
  • short static super witch synchronized
  • this throw throws transient true try typeof
  • var volatile void
  • while with

Most of the reserved words are not used in the language. The list does not include some words that should have been reserved but were not.

Names are used for statement, variables, parameters, property names, operators and labels.


JavaScript has single number type. It is represented as 64-bit floating point, the same as Java’s double. Unlike many programming language it has not integer type, So 1 and 1.0 is the same.

All you need to know about numbers is that it is a number

If a number literal has an exponent parts, then the value of the literal is computed by multiplying.

Negative numbers can be formed by using the – prefix operator.

The value NaN is a number value that is the result of an operation that cannot produce a normal result . NaN is not equal to any value including itself.


A sting can be wrapped in single quotes or double quotes. It can contain zero or more characters.

JavaScript does not have a character type. To represent a character, make a string with just one character in it. It is easy to make a new string by concatenating other string with a + operator.

Two string containing exactly the same characters in the same order are considered to be the same string.


'C' + 'A' + 'T' === 'CAT'

/* is true */

Escaped character

" (double quote)
' (single quote)
\ (backslash)
/ (slash)
b (backspace)
f (formfeed)
n (new line)
r (carriage return)
t (tab)
u (4 hexadecimal digits)


var name = expression;

A compilation unit contains a set of executable statement. In the web browser, each <script> delivers a compilation unit that is compiled and immediately executed.

When used inside of a function . the var statement defines the function’s private variable.

The switch, while, for and do statement are allowed to have an optional label prefix that interacts with the break statement.

Statement tend to be executed in the order of top to bottom. The sequence of execution can be altered by the conditional statement (if and switch), by the looping statement (while, for, and do), by the disruptive statement (break, return and throw), and by function invocation.

A block is a set of statement wrapped in curly braces. Unlike many other languages, blocks in JavaScript do not create a new scope, so variable should be defined at the top of the function, not in blocks.

The if statement changes the flow of the program based in the value of the expression. The then block is executed if the expression is truthy; otherwise, the optional else branch is taken.


/* label */
name: expression statement;
      disruptive statement;
      try statement;
      if statement;
      switch statement;
      while statement;
      for statement;
      do statement;

/* here are the values */

Disruptive Statement

break statement
return statement
throw statement


{ statement }

If Statement

if ( expression ) {
     /* block */
} else {
    /* block */

Here are the falsy values:

  • false
  • null
  • undefined
  • The empty string ‘ ‘
  • The number 0
  • The number NaN

All the other values are truthy, including true, the string ‘false’, and all objects.

Switch Statement

switch ( expression ) {
  case clause

The switch statement performs a multiway branch. It compares the expression for equality with all of the specified cases. The expression can produce a number or a string.

Case Clause

case expression : statement

A case clause contains one or more case expressions. The case expression need not be constants. The statement following a clause should be disruptive statement to prevent fall though into the next case. The break statement can be used to exit from a switch.

While statement

while ( expression ) {
/* block */

A while statement performs a simple loop. If the expression is falsy the loop will break. Wile the expression is truthy, The block will be executed.

The for statement is a more complicate looping statement. It comes in 2 forms.

The conventional form is controlled by three optional clauses; the initialization, the condition, and the increment.

do Statement

The do statement is like the while statement except that the expression is tested after the block is executed instead of before. That means that the block will always be executed at least once.

try Statement

The try statement executes a block and catches any exceptions that were thrown by the block. The catch clause defines a new variable that will receive the exception object.

throw Statement

The throw statement raises an exception. if the throw statement is in a try block, then control goes to the catch clause. Otherwise, the function invocation is abandoned, and control goes to the catch clause of the try in the calling function.

The expression is usually an object literal containing a name property and a message property. The catcher of the exception can use that information to determine what to do.

throw expression;

return Statement

The return statement causes the early return from a function. It can also specify the value to be returned. If a return expression is not specified, then the return value will be undefined.

return expression;

break Statement

The break statement causes the exit from a loop statement or a switch statement. It can optionally have a label that will cause an exit from the labeled statement.

break name;

JavaScript does not allow a line end between the break and the label.

expression Statement

An expression statement can either assign values to one or more variables or members, invoked a method, delete a property from an object. The = operator is used for assignment. Do not confuse it with the === equality operator. The += operator can add or concatenate.


The simplest expression are a literal value (such as a string or number), a variable, a built-in value (true, false, null, undefined, NaN, or infinity), an invocation expression preceded by new, a refinement expression preceded by delete, an expression followed by:

  • An infix operator and another expression
  • The ? ternary operator followed by another expression, then by :, and then by yet another expression.
  • An invocation
  • A refinement

The ? operator takes three operands. If the first operand is truthy, its produces the value of the second operand. But if the first operand is falsy, it produces the value of the third operand.

The operator at the top of the precedence list in Table 2-1 have higher precedence. They bind the tightest. The operators at the bottom have the lowest precedence. Parentheses can be used to the alter the normal precedence, so

2 + 3 * 5 === 17

(2 + 3) * 5 === 25

Table 2-1. Operator precedence

. [] ()Refinement and invocation
delete new typeof + – !Unary operators
* / %Multiplication, division, modulo
+ –Addition/concatenation, subtraction
>= <= ><Inequality
=== !==Equality
&&Logical and
||Logical or

prefix operator

+to number
!logical not

The values produced by typeof are ‘number’, ‘string’, ‘boolean’, ‘undefined’, ‘function’, and ‘object’. If the operand is an array or null, then the result is ‘object’, which is wrong.

If the operand of ! is truthy, it produces false. Otherwise, it produces true.

The + operator can produce a noninteger result even if both operands are integers.

The && operator produces the value of its operand if the first operand is falsy. Otherwise, it produces the value of the second operand.

* multiply+ add>= greater or equal=== equal|| logical
/ divide– subtract<= less or equal!== not equal&& logical and
% modulo> greater
< less

The || operator produces the value of its first operand if the first operand is truthy. Otherwise, it produces the value of the second operand.


( 'expression' ) 

Invocation causes the execution of a function value. The invocation operator is a pair of parentheses that follow the function value. The parentheses can contain arguments that will be delivered to the function.


A refinement is used to specify a property or element of an object or an array. This will be described in detail in the next blog.

.name [ expression ]


Object literal are convenient notation for specifying new objects. The names of the properties can be specified as names or as string. The names are treated as literal names, not as variable names, so the names of the properties are expressions. There will be more about object literal in the next blog.

number literal
sting literal
object literal
array literal
regexp literal

object literal

{ ' name ' : expression; }

array literal

[ ' expression ' ]

Array literals are a convenient notation for specifying new arrays.

regexp literal

|| / regexp choice / g i m ||


function literal

function name (parameter) {
 function body


( 'name' )

functions body

{ var statements statements }

A function literal defines a function value. It can have an optional name that it can use to call itself. It can specify a list of parameters that will act as variables initialized by the invocation arguments. The body of the function includes variable definition as statement. There will be more about function in future blogs.


Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: