JS Syntax

A JS program is a list of JS statements. Each statement is interpreted on the console one at a time. Each statement is delimited from another by either a semicolon (;) or a newline.

A Javascript program is read top-to-bottom, left-to-right. This is known as the execution sequence. Programming involves manipulating the execution sequence in order to achieve specific computational tasks.

Statements

JS statements are made up of:

  1. Keywords: Keywords are special words that provide commands to the interpreter. Keywords cannot be overloaded (i.e. used as variable names).

The following is the list of JS keywords (an incomplete list; new keywords are added to JS on a regular basis):

break, case, catch, continue, debugger, default,
delete, do, else, finally, for, function, if,
in, instanceof, new, return, switch, this, throw, 
try, typeof, var, void, while, with

This is not a complete list as JS continues to evolve and new keywords can be added.

  1. Values: Values can either be fixed or variable. Fixed values are called literals and variable values are called variables.

Example of literals include constants like 1, 100, 3.142 etc.

Variables are subject to the variable lifecycle that includes (in that order) -- declaration, definition and usage.

A variable is declared in a JS statement by using the var keyword. For e.g.:

var x;

Multiple variables can also be declared in one statement:

var x,y,z;

These statements serve the purpose of "declaring" these variables to the interpreter. JS does not require a declaration statement, but it is best practice to use these declarative statements, to ensure readability and ease of debugging.

The next stage in the lifecycle of a variable is definition. A variable is defined using the assignment (=) operator. For example:

x = 10;
y = "test string"

Once a variable is defined (it is undefined by default) it can be used as a placeholder for value. e.g.:

z = x + y;
  1. Operators: Operators are special characters (similar to mathematical operators) that specify a particular operation to the interpreter. The following is the list of basic operators:
Operator Description
= Assignment
+ Addition
- Subtraction
* Multiplication
** Exponentiation
/ Division
% Modulus (Division Remainder)
Table: 6.1.1 Binary Operators
Operators can be classified (based on the number of operands) as:
  • Unary Operators: These operators take in one operand as input. Some examples are:
Operator Description
++ Increment
-- Decrement
Table: 6.1.2 Unary Operators
  • Binary Operators: Binary operators require 2 operands. All standard examples mentioned before (Table 6.1.1) are binary operators. The following as some special binary assignment operators:
Operator Example Same As
= x = y x = y
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y
**= x **= y x = x ** y
Table: 6.1.3 Binary Assignment Operators
  • Ternary Operators: JavaScript supports one ternary operator -- ?:. It can be combined with an assignment operator to assign different values. For example:
var fee = (age > 18) ? '$12.00' : '$2.00'

The variable fee will be assigned to '$12.00' if age > 18 and will be assigned a value of '$2.00' otherwise.

  1. Expressions: JS expressions are statements that combine operators and operands (literals and variables). We have seen examples of this before. Here are more:
x = 6 + 8 + z;
y = a++ * 10;
  1. Comments: JavaScript supports both single-line and multi-line comments. Single line comments begin with a // and are delimited by the end of line. Multiple line comments start with /* and end with */.
// This is a single line comment

/*
This is 
a multi-line
comment
*/

Variable Names

JS variable does not have to be single characters like x, y, z etc. But can be anything as long as the following rules are followed:

  1. The variable name can contain numbers, alphabets (alpha-numeric), underscore(_) and dollar sign ($).
  2. The first character of a variable has to be an alphabet, underscore(_) or dollar sign($).
  3. Variables canot be a JS reserved keyword.
  • Variable names are case sensitive. Therefore age, Age and AGE are 3 distinct variables.
  • Variable names cannot start with numbers.
  • Variable names cannot contain spaces. Use camel-case or underscores to ensure readability of multiple word variable names (e.g.: maxValue or max_value).
  • There is no length limit on JS variable names (a good rule-of-thumb is to limit it to 32 characters).