JavaScript 5

JavaScript 5

Chapter 5 with Examples

JavaScript Syntax

JS follows a set of rules called syntax. JavaScript syntax should be followed to create correctly-constructed programs.

Variable Declaration

In maths, we use variables to hold values. It works the same way in JS, a value can be assigned to a variable. This is called ** declaring a variable**.

To declare a variable, use the var keyword to create it, and then an equal sign = to assign a value.

var num = 5; // assigns number 5 to the variable
document.write( num ); // prints the output

The num in the example above is called identifier. JS identifiers are used to name variables.

JavaScript is Case-Sensitive

JS identifiers are sensitively cased. In this example, the variables num and Num are different.

var num = 5;
var Num = 6;
var sum = num + Num; // adds the value of num and Num
document.write(sum); // prints the value of the variable sum