Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

JavaScript

Targeted at: Entry Level Trainees

Session 02: Introduction to JavaScript

© 2007, Cognizant Technology Solutions. All Rights Reserved.


The information contained herein is subject to change without notice.
C3: Protected
About the Author

Created By: Anil Kumar GV (118497)

Credential Sun Certified Java Programmer


Information:

Version and JSCRIPT/PPT/0108/1.0


Date:

2
Icons Used

Hands on
Questions Tools Exercise

Coding Test Your


Reference
Standards Understanding

A Welcome
Try it Out Contacts
Break

3
JavaScript Session 02: Overview

ƒ Introduction:
This session gives a brief overview about the salient
features of HTML, significance and understanding of
Client Side Scripting Languages.
It also explains the generic understanding of Java
Script.

4
JavaScript Session 02: Objective

ƒ Objective:
After completing this session, you will be able to:
» Identify purpose and importance of JavaScript
» List the different ways to include JavaScript code in
HTML
» State the basics of the JavaScript Programming
» Compare between JavaScript and Java
» Compare between Server Side JavaScript and Client
Side Java Script

5
About Web Scripting Language

ƒ Server-Side comprises the following languages:


» Active Server Pages (ASP)
» Java Server Pages (JSP)
» Cold Fusion

ƒ Client Side comprises the following languages:


» JavaScript
» JScript
» VBScript
» Tcl (Tool Command Language )

6
Need for Client Side Scripting

ƒ Client Side Scripting is required for the following


reasons:
» A script that is executed by the browser on a users
computer
» Instead of the entire page, part of the page is sent to
the browser when user requests for the page.
» Mostly run in response to an event
• For example: Click events, validations.

» Direct interaction with client

7
JavaScript: Overview
ƒ JavaScript is:
» Developed by Netscape
» Supports the most of the browsers
» Helps to create dynamic pages
» To design the pages according to the appropriate browser on
which it would be viewed
» You do not require access to Web server
» Interactivity
» Scripted - not compiled
» Powerful
» Object-based
» Cross-Platform
» Client and Server

8
What JavaScript can do?

ƒ JavaScript gives HTML designers a programming tool

ƒ JavaScript can put dynamic text into an HTML page

ƒ JavaScript can react to events

ƒ JavaScript can read and write HTML elements

ƒ JavaScript can be applied to validate data

ƒ JavaScript can be applied to detect the visitors browser

ƒ JavaScript can be applied to create cookies

9
What JavaScript can not do?

ƒ When you need to access other resources:


» Files

» Programs

» Databases

ƒ When you are applying sensitive or copyrighted


data or algorithms.

ƒ Your JavaScript code is open to the public.

10
JavaScript and Java: Comparison

Java Script Java


JavaScript is developed by Java is a programming language
Netscape. developed by Sun Microsystems

Based on Object Oriented Based on Object Oriented


Concepts Concepts

JavaScript is a scripting language Java cannot be parsed. It must


that is, parsed and executed by be compiled into machine
the parser. language before it is executed.

JavaScript must run inside a Web


page on a Web Browser. Web With Java stand-alone
Browser must be complaint to applications can be created
Java Script.
Putting JavaScript into Pages

ƒ Direct Insertion
Inside <head> Within <body>
» <script>Code</script>
ƒ Embedded inline
Inside other HTML tags
» <input type=“button”
onclick=‘JavaScript:alert(“Hi”);’ />
ƒ External references
In external .js files
» <script src=“JSFileName”></script>

12
Comments in JavaScript

ƒ Different comments in JavaScript are shown as

follows:

» // is single line comments

» /* */ is multiple line comment

» <!-- // -- > Non supported browser code ignore

comments

13
Programming Rules

ƒ JavaScript is case-sensitive

ƒ Ending statements with a semicolon is optional.

ƒ Open and Close braces for scoping the

embedded Java Script code is mandatory.

ƒ You can apply more than one script in a same

HTML file.

14
JavaScript Language Concepts

ƒ Java Script Language Constructs:


» Values, Variables, and Literals

» Expressions and Operators

» Regular Expressions

» Statements

» Functions

15
Values, Variables, and Literals
ƒ JavaScript does not have explicit data types
ƒ There is no way of specifying that a particular variable
represents an integer, a string or a real.
ƒ The same variable may be interpreted differently in different
contexts.
ƒ All JavaScript variables are declared applying the keyword var.
» For example: var x,y=7
ƒ JavaScript recognizes the following types of values:
» Numbers, such as 42 or 3.14159
» Logical (Boolean) values, either true or false
» Strings, such as "Howdy!"
» null, a special keyword denoting a null value

16
Values, Variables, and Literals (Contd.)
ƒ A JavaScript identifier or name must start with a letter or
underscore ("_")
ƒ Subsequent characters can also be digits (0-9)
ƒ As because JavaScript is case sensitive, letters include the
characters "A" through "Z" (uppercase) and the characters "a"
through "z" (lowercase).
ƒ Variables Scope:
» Local variable is one, which is declared inside a function

» Global variable is one, which is declared outside a function

ƒ Applying var to declare a global variable is optional. However,


you must apply var to declare a variable inside a function.

17
Values, Variables, and Literals (Contd.)
ƒ Integer Literals:
» Integers can be expressed in decimal, hexadecimal, and octal.
» Some examples of integer literals are: 42, 0xFFF, and -345.

ƒ Object Literals:
» An object literal is a list of zero or more pairs of property names and
associated values of an object, enclosed in curly braces ({}).
» String Literals
» A string literal is zero or more characters enclosed in double or
single quotation marks.
» The following are examples of string literals:
• "blah"
• 'blah'
• "1234"
• "one line \n another line”

18
Programming JavaScript

ƒ Variables:
» Howdy, numberRight, score, Ralph
ƒ Data types:
» 12, 987.654, true or false, “Texas Tech”
ƒ Operators:
» >=, ==, +=, ||, <, >, and so on.
ƒ Control statements:
» If, if….else, for, and so on.
ƒ Keywords:
» Var, true, false, new, return

19
User Defined Functions in JavaScript

ƒ Following is the syntax to define the function


in JavaScript:
function functionName(var1,var2……..)
{
some code……
return <Data> //optional
}
» The return statement is applied to specify the value
that is returned from the function.

20
Conditional Statements: If..else
if..else Syntax
if (condition){
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
If.. Else if…Else Syntax
if (condition1) { code to be executed
}
else if (condition2) { code to be executed
}
else { code to be executed
}

21
Conditional Statements: Switch
Switch Syntax:

switch(n) {
case 1: execute code block 1 break ;

case 2: execute code block 2 break ;

default: code to be executed if n is different


from case 1 and 2
}

22
Control Statements
ƒ The for loop is applied when you know in
advance, how many times the script should run.
» For loop Syntax:
var initval;
for(initval=startvalue;initval<=endalue;initval=initv
al+incrval)
{
code to be executed
}

23
Control Statements (Contd.)

While Syntax:
while (initval<=endvalue)
{
code to be executed
}

Do..While Syntax:

do {
code to be executed
} while (var<=endvalue)

24
Control Statements: For…IN

ƒ The for..in statement is applied to loop


(iterate) through the elements of an array or
through the properties of an object.
» For..In Syntax:

for (variable in object)


{
code to be executed
}

25
Q&A

ƒ Allow time for questions from participants

26
Test Your Understanding

1. What is the difference between JavaScript


and Java?
2. Can you have an stand alone application built
using Java Script?
3. Is Java Script case sensitive?

27
JavaScript Session 02: Summary
ƒ Client Side Scripting is a script that is executed by the browser
on a users computer
ƒ Server-Side scripting language comprises of Active Server Pages
(ASP), Java Server Pages (JSP), Cold Fusion
ƒ Client Side scripting language comprises of JavaScript, Jscript,
VBScript, Tcl (Tool Command Language )
ƒ JavaScript is a scripting language that is, parsed and executed by
the parser. It must run inside a Web page on a Web Browser
ƒ JavaScript can put dynamic text into an HTML page
ƒ Java Script Language Constructs consist of Values, Variables, and
Literals, Expressions and Operators, Regular Expressions,
Statements, and Functions

28
JavaScript Session 02: Source

ƒ www.w3schools.com

ƒ http://www.yourhtmlsource.com/javascript/ba
sicjavascript.html

Disclaimer: Parts of the content of this course is based on the materials available from the Web sites and
books listed above. The materials that can be accessed from linked sites are not maintained by
Cognizant Academy and we are not responsible for the contents thereof. All trademarks, service marks,
and trade names in this course are the marks of the respective owner(s).

29
You have completed the
Session 02 of
JavaScript

© 2007, Cognizant Technology Solutions. All Rights Reserved.


The information contained herein is subject to change without notice.

You might also like