JavaScript Syntax


JavaScript syntax is the rules, how JavaScript programs are constructed.


JavaScript Statements

JavaScript statements are separated by semicolon:

var x = 5;
var y = 6;
var z = x + y;

JavaScript Values

The JavaScript syntax defines two types of values: Fixed values and variable values.

  • Fixed values are called literals.
  • Variable values are called variables.

JavaScript Literals

The most important rules for writing fixed values are:

  • Numbers are written with or without decimals:
  • 23.50 105
  • Strings are text, written within double or single quotes:
  •  "Sachin"
  • Expressions can also represent fixed values:
  • 2 + 3  10 * 4

JavaScript Variables

variables are used to store data values.

JavaScript uses the var keyword to define variables.

An equal sign is used to assign values to variables.

var x;
x = 6;

JavaScript Operators

JavaScript uses an assignment operator ( = ) to assign values to variables:

var x = 5;
var y = 6;

JavaScript uses arithmetic operators ( + - * / ) to compute values:

(5 + 6) * 10

JavaScript Keywords

JavaScript keywords are used to identify actions to be performed.

The var keyword tells the browser to create a new variable:

var x = 5 + 6;
var y = x * 10;

JavaScript Comments

Not all JavaScript statements are "executed".

Code after double slashes // or between /* and */ is treated as a comment.

Comments are ignored, and will not be executed:

var x = 5;   // I will be executed

// var x = 6;   I will NOT be executed

JavaScript is Case Sensitive

All JavaScript identifiers are case sensitive.

The variables lastName and lastname, are two different variables.

lastName = "Yadav";
lastname = "Singh";

JavaScript Character Set

avaScript uses the Unicode character set.

Unicode covers (almost) all the characters, punctuations, and symbols in the world.


Share this article on