JavaScript syntax

Syntax

The syntax of JavaScript is the set of rules that defines correctly structured JavaScript program. The statements which JavaScript program consists are placed inside <script> and </script> HTML tags. The JavaScript file is saved with a.js extension.

Let us write some statements of JavaScript program.

var x=8;
var y=10;
var sum=x+y;
document.write(sum);   // It will print sum of two numbers.

JavaScript is case-sensitive

JavaScript is totally a case sensitive language which means once you have declared a variable in lowercase letters, you cannot change it into upper letters later. For example myBox variable cannot be used as MyBox.

var myBox="Hello World";
document.write(MyBox);   //Wrong
document.write(Mybox);   //Wrong
document.write(myBox);   //Right

How to write comments in JavaScript?

The comments in JavaScript are simply texts which are completely ignored by interpreter. Comments are necessary to show the meaning of code which can be used in future.

JavaScript allows single line and multi line comments. The single line comments after // whereas multi line comments start with (/*) and ends with (*/)

// This will produce Hello World.
document.write("Hello World!");

/* This is my first
javascript program*/

The script tag takes two important attributes:

Language 

This attribute specifies what scripting language you are using. Typically, its value will be JavaScript. Although recent versions of HTML (and XHTML, its successor) have phased out the use of this attribute.

Type 

This attribute is what is now recommended to indicate the scripting language in use and its value should be set to “text/JavaScript”.