Introduction to JavaScript : Syntax

Syntax

Java script has a very simple and easy to learn syntax. You can write any JavaScript statement and execute it by placing it within the <script>... </script> tags in HTML documents. The <script> tag indicates to the browser that it has reached a point where all the text between these <script> tags are sets of statements that need to be executed. A simple example of Java Script syntax is as follows:

<script>
JavaScript code  
</script>   

Let us illustrate the syntax of JavaScript with a simple program that prints “Hey there!”.   

<html> 
   <body>  
      <script> 
         <!-- 
            document.write("Hey there!") 
         //--> 
      </script>  
   </body> 
</html>

The output produced by this code will be

Hey there! 

White Spaces, Line Breaks and Semicolons  

JavaScript ignores irrelevant spaces, tabs, and new lines within a program. This allows free formatting and unrestricted levels of indentations to suit one's readability needs.

Simple statements in JavaScript and many other programming languages like java, C, etc. usually end with a semicolon. In JavaScript, you may omit semicolons if each instruction is placed on separate lines. For example, the following code explains the whole procedure described above.

<script>
    var x = 15
    var y = 25
</script>  

When multiple statements are placed are on a single line, then semicolons are necessary to determine the end of the statements. 

<script language = "javascript" type = "text/javascript">
    var x = 15; var y = 25; var z = 30
</script> 

course logo
Introduction to JavaScript
Number of sections:
10
Content length:
Delivery formats: