JavaScript Syntax

JavaScript uses the Unicode character set. Every programming language has its own set of rules called syntax and Javascript is no exception. A programming language is a set of statements that conform to a set of rules known as grammar or syntax.

Every statement of Javascript ends with a semicolon as shown below.

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

JavaScript Statements

JavaScript statements are made of literals, operators, expressions, keywords, and comments.

JavaScript Literals

Constant values such as 3.14 or “Tom Clancy” are called literals. Rules for writing these values are different for different types of data:

Numbers literals can be written with or without decimals


Text values are called Strings and written within double(“) quotes or single(‘) quotes:

"Tom Clancey";
'Bill Gates the role model';

JavaScript Variables

All programming language use variables to store data. In JavaScript var keyword is used to declare/define variables.
var keyword is optionally followed by equal (=) sign to assign values to variables.

In below example, variable x is defined with initial value of 5 whereas var y is defined without any initial value.

<!DOCTYPE html>

<h1>JavaScript Variables</h1>

<p>In this example, x is defined as a variable and assign value 5.</p>

<p id="demo"></p>

var x;
x = 5;
document.getElementById("demo").innerHTML = x;


Download the code Run the code

JavaScript Operators

Javascript has many types of operators like assignment operator(=) and arithmetic operator( + – * / ). Operators ‘operate’ on variables, literals and expressions and return the results in place. In the below sample, x+20 is computed to 25 and the result is stored in y. In the next line (10 + 90)/y is computed as 100/y which is further computed as 5 and then stored in variable z.

var x = 5; 
var y = x + 20;
var z = (10 + 90)/y;

JavaScript Expressions

Javascript Expressions are combination of variables, values and operators. An expression always evaluates to some value. For example, 10 * 10 evaluates to 100.

Variables can be part of Expressions

<!DOCTYPE html>

<h1>JavaScript Expressions</h1>

<p>Result of Expression is a value.</p>

<p id="demo"></p>

document.getElementById("demo").innerHTML = 10 * 10;


Download the code Run the code

JavaScript Keywords

Like any computer language, JavaScript keywords are reserved words used by the language. These keywords can’t be used by the programmer to define new variables or functions. The var keyword in below statement tells the browser to create a new variable:

var KH_Count = 0;  // correct
var function;      // wrong since function is a keyword and can't be used as a variable name

The following are some of the keywords defined by the JavaScript language:


break case catch class const continue debugger default delete do else export extends 
finally for function if import in instanceof new of return super switch this throw 
try typeof var void while with yield
<<< External JavaScript JS File – Advantages, Disadvantages, Syntax, AttributesJavaScript Statements >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .