Sie sind auf Seite 1von 18

JAVA Script

JavaScript is a scripting language JavaScript was designed to add interactivity to HTML pages A scripting language is a lightweight programming language JavaScript is usually embedded directly into HTML pages JavaScript is an interpreted language (means that scripts execute without preliminary compilation) Everyone can use JavaScript without purchasing a license

JavaScript's official name is ECMAScript. ECMAScript is developed and maintained by the ECMA organization. ECMA-262 is the official JavaScript standard. The language was invented by Brendan Eich at Netscape (with Navigator 2.0), and has appeared in all Netscape and Microsoft browsers since 1996. The development of ECMA-262 started in 1996, and the first edition of was adopted by the ECMA General Assembly in June 1997. The standard was approved as an international ISO (ISO/IEC 16262) standard in 1998. The development of the standard is still in progress.

JS can do.
JavaScript gives HTML designers a programming tool JavaScript can put dynamic text into an HTML page JavaScript can read and write HTML elements JavaScript can be used to validate data JavaScript can be used to detect the visitor's browser JavaScript can be used to create cookies

Syntax:
<script type="text/javascript"> ... </script> Eg: <html> <body> <script type="text/javascript"> document.write(Welcome to IIST!"); </script> </body> </html>

Where JS can be?


Scripts in <head>
Scripts to be executed when they are called, or when an event is triggered, go in the head section.

Scripts in <body>
Scripts to be executed when the page loads go in the body section.

Scripts in <head> and <body>


You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.

External JavaScript
<script type="text/javascript" src="xxx.js"></script>

JavaScript Statements
A JavaScript statement is a command to a browser. JavaScript is Case Sensitive JavaScript Code - is a sequence of JavaScript statements. JavaScript Blocks - statements can be grouped together in blocks.

JavaScript Comments
// Single line comment /* Multi - line comment */
Comments at the end of the line //

JavaScript Variables
Variable names are case sensitive (a and A are two different variables) Variable names must begin with a letter or the underscore character Syntax:
var x; x=5; x=IIST; var x=10;
Note: Variable can be assigned without declaration.

JavaScript Operators
Arithmetic Operators + , - , * , / , % , ++ , - Assignment Operators = , += , -= , *= , /= , %= ,.= Comparison Operators == , != , > , < , <= , >= Logical Operators && , || , ! Conditional Operator variablename=(condition)?value1:value2

The + Operator Used on Strings


str1="What a very"; str2="nice day"; str3=str1+str2;

Adding Strings and Numbers


x=5+5; x="5"+"5"; x=5+"5"; x="5"+5;

Control Statements
(i) if statement if (condition) code to be executed if condition is true; (ii) if...else statement if (condition) code to be executed if condition is true; else code to be executed if condition is false;

(iii) If...else if....else statement


if (condition) code to be executed if condition is true; else if (condition) code to be executed if condition is true; else if (condition) code to be executed if condition is true; else code to be executed if condition is false;

(iv) Switch statement switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; default: code to be executed if n is different from both label1 and label2; }

Loops
(i) while loop while (condition) { code to be executed; } (ii) do While loop do { code to be executed; }while (condition);

(iii) for loop for (init; condition; increment) { code to be executed; }


(iv) break and continue Statements break will terminate the loop continue will break the loop for that iteration and continue with the next value

(v) for - in Statement for (variable in object) { code to be executed }

JS Function
Syntax: function functionname(var1,var2,...,varX) { some code } Function call: functionname(var1,var2,,varX);

Das könnte Ihnen auch gefallen