Saturday, July 3, 2010

Basic of JavaScript

Java Script Syntax and Features

 1. There is no need to declare variable, however
you can


     define variable with var keyword.

 2. Curl braces ({}) are separated two code
blocks.


 3. White space and carriage return are not count
in code statement.


 4. Semicolon (;) terminate a statement.

 5. For single line comment use double slash
(//).


 6. For multi line comment use /* and */ sign.

 
Variable name rules

Variable are place holder of value used in run time by the program, every
variable has an unique name within the scope of execution, while declaring the
variable always keep un mind.

 1. Must start with number and underscore (_)

 2. Use any alphanumeric character.

 3. Do not use any special character and white space.

 4. Variable name are case sensitive.

 5. Do not use keyword as variable name.



Operator and order of precedence


1

.

[]

()





















2

++

--

!~





















3

*

/

%





















4

+

-























5

<<

>>

>>>





















6

<

>

<=

>=



















7

==

!=























8

&

























9

^

























10

|

























11

&&

























12

||

























13

?:

























14

=

+=

-+

*=

/=

%=

<<=

>>=

>>>=

&=

^=

!=






























String
Operators




+

Concatenation


Comparison Operators




==

Equal To



!=

Not Equal To



<

Less Than



>

Greater Than



>=

Greater than or equal to



<=

Less than or equal to


Arithmetic Operators




*

Multiplication



/

Division



+

Addition



-

Subtraction
Logical
Operators




&&

AND
Assignment
Operator




=

Assignment

Keyword List

In
JavaScript there are 50 keywords, this are reserve word in the language you can
not use this keyword in naming of variable or function.



abstract

float

public



boolean

for

return



break

function

short



byte

goto

static



case

if

super



catch

implements

switch



char

import

synchronized



class

in

this



const

instanceof

throw



continue

int

throws



default

interface

transient



do

long

true



double

native

try



else

new

var



extends

null

void



false

package

while



final

private

with



finally

protected


Conditional
Construct


 1. if.. else ..

 2. switch case



Looping Construct

 1. for

 2. while 



User Define Function

 function <functionname>(argument)

 {

   //function body

  return <value>

 }

Some Sample Small Programmes



1. Writing text on the browser window 
<html>
<body>
<script language="JavaScript">
document.write("Hello World!")
</script>
</body>
</html>

2. Writing Formatted Text on the Browser 
<html>
<body>
<script language="Java Script">
document.write("<h1>Hello World!</h1>")
</script>
</body>
</html>

3. Using HEAD Section
<html>
<head>
<script language="JavaScript">
function message(){ 
alert("This alert box was called with the onload 
event")
}
</script>
</head>
<body onload="message()">
</body>
</html>

4. Using BODY Section 
<html>
<head>
</head>
<body>
<script language="JavaScript">
document.write("This message is written when the 
page loads")
</script>
</body>
</html>
<!--Example E Using External script -->
<html>
<head>
</head>
<body>
<script src="myscript.js">
</script>
<p>The actual script is in an external script 
file called "myscript.js".</p>
</body>
</html> 

Try yourself.
1. Load any text editor (Notepad, WordPad, Word, 
TextPlus)
2. Create the HTML file and save it, with .HTM or 
.HTML extension. and write following HTML and javascrpt.
   <html>
   <body>
   <script language="JavaScript">
   document.write("Hello to JavaScript World!")
   </script>
   </body>
   </html>
3. Open saved file in Browser (IE, Netscape) to 
view it will display Hello to JavaScript World!.

Java Scripting

What is Java Script?
JavaScript is an object-based scripting language designed primarily for HTML (Hypertext Markup Language), It supports both IE (internet Explorer) and Netscape Navigator and all other major web browser. Java script can be used for both client side as well as server side in web development. It is not a programming language it can not create executable code while compiling, it is embed in document and while pursing the html document it executes the script, it is an object-based scripting language, while reasonably simple in syntax, construction, and features, the object-based nature of JavaScript still offers programmers significant power and flexibility through the ability to create functions and new objects.
An object in the context of JavaScript, is a collection of properties and methods consisting a set of defined characteristics that you can view and modify, and which you can interact. Methods are the techniques that are used to perform action involving objects and properties. For example statement like Document. Write("Welcome to Javascript") here Document is the object, Write is the method of Document object, JavaScript follows event driven sequence. the events determine the program flow and event handlers determine what happen when these event occurs.


Where to Write Java Scrip
You can write javascript in body or head section depend on your requirement or you can write java script in a sepeate file with extension ".js" and includes it in your HTML document with