Writing JavaScirpt

Writing JavaScirpt

Writing JavaScirpt

Writing JavaScirpt

Writing JavaScirpt
Writing JavaScirpt


 

Writing JavaScript


JavaScript code is typically embedded in the HTML, to be interpreted and run by the client's browser. Here are some tips to remember when writing JavaScript commands.

• JavaScript code is case sensitive

• White space between words and tabs are ignored

• Line breaks are ignored except within a statement

• JavaScript statements end with a semi- colon ;

The SCRIPT Tag


The <SCRIPT> tag alerts a browser that JavaScript code follows. It is typically embedded in the HTML.

<SCRIPT language = "JavaScript">
     statements
</SCRIPT>

Example:

 

<HTML>
    <HEAD>
    </HEAD>
<BODY>
    <SCRIPT language = "JavaScript">
        alert("Welcome to the script tag test page.")
    </SCRIPT>
</BODY>
</HTML>

 

Implementing JavaScript

There are three ways to add JavaScript commands to your Web Pages.
    • Embedding code
    • Inline code
    • External file

External File

You can use the SRC attribute of the <SCRIPT> tag to call JavaScript code from an external text file. This is useful if you have a lot of code or you want to run it from several pages, because any number of pages can call the same external JavaScript file. The text file itself contains no HTML tags. It is call by the following tag:

<SCRIPT SRC="filename.js">
</SCRIPT>
 

 

Guide Lines for JavaScript

 

Whitespace and Line Breaks:

JavaScript ignores spaces, tabs, and newlines that appear in JavaScript programs.

Because you can use spaces, tabs, and newlines freely in your program so  you are free to format and indent your programs in a neat and consistent way that makes the code easy to read and understand.
 

Semicolons are Optional:

Simple statements in JavaScript are generally followed by a semicolon character, just as they are in C, C++, and Java. JavaScript, however, allows  you to omit this semicolon if your statements are each placed on a separate line. For example, the following code could be written without semicolons.


<script language="javascript" type="text/javascript">
<!--
var1 = 10 var2 = 20
//-->
</script>


 

But when formatted in a single line as follows, the semicolons are required:
 


<script language="javascript" type="text/javascript">
<!--
var1 = 10; var2 = 20;
//-->
</script>

 

Note: It is a good programming practice to use semicolons.


Case Sensitivity:

JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.

So identifiers Time, TIme and TIME will have different meanings in JavaScript.

NOTE: Care should be taken while writing your variable and function names in JavaScript.