JavaScript Syntax

To impletment JavaScript we can use JavaScript statements That are placed within the <script>...</script>.We can Place <script> tags anywhere within your web page,But We recommended that you should place it within the <head> tags of HTML.

JavaScript Syntax will appear as follows.

<script>
   Your JavaScript code
</script>

Whitespace and Line Breaks In JavaScript

JavaScript ignores tabs,spaces and newlines that appear in JavaScript Code.We can use tabs,spaces and newlines freely in our code.

Semicolons are Optional In JavaScript

We can Ignore semicolon in JavaScript if each of JavaScript statements are placed on a separate line.

<script language="javascript" type="text/javascript" >
   <!--
      myVar1 = 10
      myVar2 = 20
   //-->
</script>

But When We formatted statements in a single line then we must use semicolons.

<script language="javascript" type="text/javascript" >
   <!--
      myVar1 = 10;myVar2 = 20;
   //-->
</script>

Comments In JavaScript

JavaScript comments are used to prevent execution of code and explain JavaScript code to make it more readable.

Single Line Comments In JavaScript

Single line comments in JavaScript start with //.Text between // and the end line will be ignored by JavaScript.

Example

<script language="javascript" type="text/javascript" >
var x = 7;      // Declare x, give it the value of 7
</script>

Multi-line Comments In JavaScript

Multi-line comments in JavaScript start with /* and end with */. Any Text between in it will be ignored by JavaScript.

Example

<script language="javascript" type="text/javascript" >
/*
This is Example of 
Multi-line Comments.
*/
var x = 12;   
var y=16;
</script>