Chapter 1. Introduction to JavaScript |
|
Chapter 2. Variables and Data Types |
|
Chapter 3. Operators |
|
Chapter 4. Statements |
|
Chapter 5. Functions |
|
Chapter 6. Event Handlers |
Chapter 7. JavaScript in Action - Examples |
What is JavaScript? |
JavaScript is Netscape's cross-platform, object-based scripting language. It is not useful as a standalone language. It can be embed with a HTML file to give an interaction to the web pages. JavaScript can also be thought of as an extension to HTML. The JavaScript is interpreted by the client web browser and solve our purpose. By using JavaScript, we can customize our web pages on the fly, write event handlers, validate data at the client-side and perform other client-side computation. JavaScript brings interaction to the web pages. For example, when a user press the SUBMIT button in a HTML form, if we want to check the value of a mandatory text field, we no need make a server call for this purpose initially. If it is something simple, we can do the validation locally, ie., in the client-side itself using JavaScript and give an alert to the user asking him to enter a value in that field. JavaScript can also be used in a number of ways to spice up our page. Now-a-days, almost all the web applications needs this type scripts, for various purpose. There are few scripts of this kind. The one more is VB Script by Microsoft. But JavaScript is one widely used. The advantage of JavaScript over other scripting languages of this kind will be explained in due course. The current version of JavaScript is 1.5. |
Is it Java or how it is related to Java? |
JavaScript and Java are similar in some ways but fundamentally different in others. The JavaScript language resembles Java but does not have Java's static typing and strong type checking. JavaScript supports most Java expression syntax and basic control-flow constructs. |
What should I know to learn JavaScript? |
Some basic HTML is enough. The knowledge of any programming language, will make the understanding easier. But, it is not a must. Nothing to worry. |
What are the software I need to install for JavaScript? |
Nothing more than
|
Something more about JavaScript. |
|
What is the basic structure of JavaScript? |
<script type="text/javascript" language="javascript"> ... JavaScript Code ... </script> Also can also specify an external JavaScript file as, <script language="javascript" src="myJavaScript.js"></script>
|
Where to place my JavaScript in a html? |
Anywhere in the html. But the recommended place is inside the <HEAD> </HEAD> tags of your html. This way, your functions are loaded before the page begins to display, and you won't see all kinds of JavaScript errors. Example: <HTML> <HEAD> <TITLE>My Title</TITLE> <script language="javascript"> function cool() { ....... } </SCRIPT> </HEAD> Here, you may ask me, what will happen if suppose somebody use some old browsers? The answer is, if the old browser don't recognize the tag <SCRIPT> and rather than performing your JavaScript, they will display your script as text in the browser. OK. Then how to avoid this? See the following: <script language="javascript"> <!-- Hide from older browsers ...... JavaScript Statements ....... // end hiding--> </SCRIPT> Before start programming, we see some examples in the examples page. |
Variables |
Variables are used for storing values. These values can be changed continually during the script execution. A variable name can consist of alphanumeric characters and the underscore. It cannot begin with a numeral. Variable names are case sensitive. Valid variable names: myName my_name total _total Average Invalid variable names: 100_numbers (starts with a numeral) rate%_of_inflation (illegal character) |
Assigning values to a variable |
var yourName; // undefined var message; // undefined. var total = 5000; var ourSite = "www.programmersworld.biz"; We can also declare variable without specifing "var", ie, like yourName; message; total = 5000; ourSite = "www.programmersworld.biz"; |
Simple Data Types |
There are three simple data types are in JavaScript:
|
Special Data Types |
There are two special data types are in JavaScript:
|
Complex Data Types |
There are three complex data types are in JavaScript:
|
Data Type Conversion |
JavaScript is a dynamically typed language. ie., we need to specify the data type of a variable when we declare it, and data types are converted automatically as needed during script execution. Example: var myBus = 857; Later, we can assign a string value to this variable like, myBus = "This bus going to Yishun ..." Because JavaScript is dynamically typed, this wouldn't cause an error. Examples: x = "The answer is " + 42 // returns "The answer is 42" y = 42 + " is the answer" // returns "42 is the answer" |
Arithmetic Operators | ||||||||||||||
|
||||||||||||||
Comparison Operators | ||||||||||||||
|
||||||||||||||
Boolean Operators | ||||||||||||||
|
||||||||||||||
Assignment Operators | ||||||||||||||
|
||||||||||||||
Special Operators | ||||||||||||||
|
Statements define the flow of a program. Here we are going see conditional statements, loops and how to comment out a line or a block. |
Conditional Statement: if ... else |
As seen in many programming languages, based on a condition, it will execute if part or (optionally) else part. if (condition) { ... } else { ... }Here, the else part is optional. |
Conditional Statement: switch |
The 'switch' statement is a shorthand of using many 'if' statements.
switch (expression) { case match1: ... statement1 ... break; case match2: ... statement2 ... break; case match3: ... statement3 ... break; default: ... default statement .. }Note: Look at the "break". Use of this break is to avoid the execution of any cases below the matching case. Use of the "default" is optional. |
Loop: for |
for (initial-statement; condition; increment) { ...statements ... } |
Loop: do ... while |
do { ... statements ... } while (condition) |
Loop: while |
while (condition) { ... statements ... } |
break and continue |
Both of these statements are used to jumb out of an iterating loop. Example: for (i = 0; i < 10; i++) { if (i == 5) { alert ("i == " + i); break; } else { continue; } } |
Comments |
Comments can explain the action, like a color commentary, which can be a great help in understanding the code and debugging. Comments are optional for a program. Example: // This is single line comment /* This is multiline comment. */ |
A function is group of statements. Functions are the fundamental building blocks of any programming language.
|
Creating functions |
Syntax to define a function:
function functionName(argument1, argument2, ...) { ... statements ... } Arguments are optional to a function. Example: 1 function checkEntries(x, y) { if (x > y) { return true; } else { return false; } } Example: 2 function printErrorMessage() { document.write("Error:..."); } |
Calling functions |
We can call function
isValueOK = checkEntries(25, 40); or if (checkEntires(25, 40)) { .... } else { printErrorMessage(); } In JavaScript, mostly the functions are called inside an event handler. Next lets see about event handlers. |