Javascript Basic Syntax

As we already know, Javascript is a high-level programming language. It has a rich syntax that enable highly human-readable. The rich syntax made the language easy, simple but yet it is very powerful.

The entire google map project uses Javascript as the core language both for client-side and some of server-side.

Javascript is constructed by a set of rules and syntax. Properly written JS source code can be translated by Javascript interpreter. If you mistake any single syntax then proper execution will be errors.

Mistaking syntax in programming called syntax error.

Javascript Syntax

Like all other high-level programming languages, Javascript also has syntax rule for writing source files. Most common of them,

Variable Declaration

The keyword var is used in javascript to declare a variable with syntax var varName; or var varName1, varName2, varName3, ...; See detail example below:


var mike; // single variable without value
var mike, bob, alex; // multiple variable without value
var mike = 12; // single variable with value assigned
var mike = 12, bob = 15, alex = 23; //multiple variable with value assigned

The var keyword is used to define variables. We will learn everything about variable in a later chapter.

Javascript Values

In the above Javascript code example, in the statement var mike = 15; here 15 is the value of the mike variable. In general, values are assigned to a variable using javascript assignment ( = ) sign.

Javascript Semicolons ( ; )

In the above example, notice that we have put a semi-colon ( ; ) after every statement defined. It actually refers to terminate a statement.

Remember: If you miss this code while programming in Javascript, a syntax error will occur.

Javascript Assigning Values

The equal to ( = ) sign is used to assign a value to a variable in Javascript. var myVar = 10; here ( = ) does not mean equal to. It means 10 will be assigned to myVar variable.

Javascript Commenting

Comments are useful for manipulating the large scale of codes for easily reading the codes. See Javascript comment example below,


//It is comment and it is not interpreted by JS interpreter.
/* This is multi-line comment
for writing large comment */
document.GetElementbyId.innerHTML("Hello, World"); //another comment

From the above example, we see in Javascript, comments are written in /* ... */ or after // comment....

We will learn more about comment in later chapters

Javascript Identifier

Javascript identifiers are the names given by a users. It is the opposite of Javascript Keywords. In general, identifiers are used to name the variables.

var myVar = 10; here myVar is the declared variable and it is also called an identifier.

There are some rules you have to follow before defining an identifier. We will learn them in a later chapter.

Javascript Keywords

Keywords are the sets of system word that used in Javascript for handling different type of tasks. For example, declaring a variable var myVar; here var is the only keyword you have to use to declare variables.

Similarly, function is a keyword for defining function,


var myVar = "Hello, World!";
function myFunction() {   //function is a keyword for defining function
  //Javascript code goes here

To learn more about keywords read: Javascript Reserved Keywords

Javascript Letter Case

Javascript is case sensitive. myVar and Myvar is different variable identifier.

« Previous Next »

Posted in