JavaScript Syntax

JavaScript was originally named LiveScript, was developed by Netscape. JavaScript is light weight client side scripting programming language used to build interactive web sites. It can be executed by all modern browsers like Internet Explorer, Google Chrome, Mozilla Firefox, Safari etc.It is case-sensitive language.

JavaScript can be divided into three parts: the core, client side and server side.

  1. The core includes operators, expressions, sub programs and statements.
  2. The client side JavaScript includes objects that provide support for browser and user to interact with each other.
  3. The server side JavaScript is collection of objects that make the language useful on the server.

JavaScript Syntax

Syntax of JavaScript can be written as shown below :

<script language="javascript" type="text/javascript">
   JavaScript code
</script>

The script tag contains following two attributes:

  • language: It specifies scripting language of the contents. Typically value will be javascript. Its value is identifier for the language.
  • type: The scripting language is specified as content type . It should be set to “text/javascript”. There is no default value for this attribute.

JavaScript Syntax Example

<!DOCTYPE html>
<html>
<body>
<head>

    <script lang="javascript" type="text/javascript">
       document.write("Welcome to Java Scripting langauge!!!");
     </script>
</head>
</body>
</html>

JavaScript syntax determines structured of JavaScript program. The function document.write() writes HTML expressions or JavaScript code to the document. After executing the above program, we will get output as follows:

Welcome to Java Scripting language!!!

Whitespace and Line Breaks

A whitespace character is a empty space on the screen. In includes spaces, tabs and line break characters. In JavaScript use of excessive whitespace is ignored.
For instance, a b c d is written as abcd. We can use these characters to format the programs in neat way that makes code easy to read and understand.

Comments in JavaScript

The comments are used to make code more readable and easy understandable.

  • The Single Line Comment : In JavaScript single line comment start with
    //(double slashes) and no required at the end.
    Example:

    <script lang="javascript" type="text/javascript">
       //Display this message in the output
        document.write ("Welcome to Java Scripting langauge!!!");
    </script>
    
  • Multi-Line Comments: This comment starts with /* and end with */.
    Example:

    <script lang="javascript" type="text/javascript">
          /*
              Following statements will <br>
              display the messages on the screen. </b>
            */
    	document.write ("Hello World!!!");
    	document.write ("Welcome to Java Scripting langauge!!!");
    
    </script>
    
  • Comments at end of line: The end line comment can be written at the
    end of executed statement. End line comment start with //(double slashes) and no required at
    the end.
    Example:

    <script lang="javascript" type="text/javascript">
       document.write ("Welcome to Java Scripting langauge!!!");//Display this message in the output
    </script>
    

Case Sensitivity

The JavaScript is case sensitive language which start with name of the constructor with a capitalized letter and name of the function or variable with lower case letter. A variable named myProgram and MyProgram are two different variables. For instance, if we declare variable called myVariable in JavaScript, we have to use myVariable rather than we can’t use MyVariable, MYVARIABLE or any other combination.

Care should be taken while writing variable or function names in the JavaScript.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*