SlideShare une entreprise Scribd logo
1  sur  31
JavaScript language fundamentals
JavaScript is Java code written
in browsers!
JavaScript can be
used to do server
side code as well
JavaScript is
programming
language
JavaScript
make
webpage
look more
beautiful!
JavaScript are
very slow and
unpredictable!
Myths or Facts Let us discover
JavaScript
 Is a scripting language developed by Netscape, and
later standardized by W3C.
 It was originally called LiveScript.
 Used not only for web client and server side web
programs, but also for iPhones, Adobe Photoshop,
Adobe Flash action script to name a few.
 JavaScript on web page works with HTML and CSS
to create a DHTML page.
Our focus
Scripting and Programming language
 Script are line of code that does not execute stand-
alone. They run on browser (client-side) or
application server (server-side) or on top of some
other application.
 They are interpreted at runtime and are not
compiled.
 Usually are loosely-typed language.
 Examples: JavaScript, JScript, VBScript, PHP
Is JScript not
same as
JavaScript?
JScript is
Microsoft version
scripting
language which is
very similar to
JavaScript
Oh! I
thought it
was
VBScript
Microsoft produced 2
scripting languages-
VBScript –(syntax similar
to VB)and JScript
JavaScript in a Web Page
 To create more interactive pages- client side validations
etc.
 To generate html dynamically.
 Event handling
 To enhance browser capabilities by giving it a better
look – printing on status bar etc.
 Interaction with embedded components like applets and
active x controls.
Response
Client
Request
HTML
file
JavaScri
pt
HTML files
JavaScript Execution
Server side code
executes on the
server and sends
the response
Script executes
locally and
interacts with
the browser
Web Server
Server side code
Other
files
So JavaScripts on client side executes faster than server-side code.
Where will the code be written?
 Email format
validation
 Password validation
 Changing the colour
of the page based on
user’s input
 Displaying different
page based on the
user’s role.
 Refreshing just part of
the page
Client-side
JavaScript code
Server-side code
Language Features
 Syntax similar to C++ and Java
 Case sensitive
 Loosely typed
 Interpreted
 Platform independent
 Object-based language
 Semicolon, as separator for multiple statements
in the same line.
Object-based language
 Also called prototype-based object oriented language
 Object-based language
 JavaScript objects are associative arrays
 functions as object constructors and
methods
 prototypes instead
of classes for inheritance.
More on this later
Simple Scripts in HTML
<HTML><HEAD><TITLE>Hello</TITLE></HEAD>
<BODY>
First java script code<br>
<SCRIPT type="text/javascript” >
//Java script single line comment
alert(“Hello java script”);
/* java script script
multi-line comment */
</SCRIPT>
</BODY>
</HTML>
popup
What will happen if you
run this on a browser
that does not support
JavaScript?
NOSCRIPT and Hiding scripts
 Because some browsers don’t support
JavaScript it is wise to put the JavaScript code
inside HTML commented tags.
<SCRIPT type="text/javascript” >
<!—
alert(“Hello java script”)
-->
</SCRIPT>
<NOSCRIPT>
Java script is not supported
</NOSCRIPT>
• The text inside the <NOSCRIPT> tag will not be
processed by the JavaScript aware browsers.
External Script
 Scripts can also be written in a separate file and
can be referenced in a HTML file.
<HTML><HEAD><BODY>
<SCRIPT type="text/javascript”
SRC=“jsfile.js”>
</SCRIPT>
</BODY>
</HTML>
alert(“Hello”);
jsfile.js
Path can be
1. Absolute path
http:://server1/get.jsp
2. Root relative:
/scripts/jsfile.js
3. Document relative
../scripts/jsfile.js
Why would you want
to write JavaScript in
another file?
Placeholders for scripts within HTML
 Inside head
 only declarations. Declarations could for variables or functions.
 No standalone executable statements must appear
 Inside the body
 any statements can appear
 standalone executable statements inside the body are interpreted in
place where it appears.
 Along with the event handler
 Script expression can be written as a value when an event like button
click happens.
Data types
 Data types supported by Java Script are
a) Numeric – integer and floating point numbers
(64 bit, IEE754 floating point)
alert(5+154e-2);
b) String-
alert(“Hello”) ;or
alert(‘Hello’) ;
c) Boolean- true, false
alert(’God gives every bird it's food
but he doesn't throw it into it's
nest’); gives me error
Hey! use escape sequences
alert(’God gives every bird
it's food but he doesn't
throw it into it's nest’);
Yes ! escape sequences can be used or you
could also use double quote for string
literal.
alert(“God gives every bird it's
food but he doesn't throw it
into it's nest”);
Variables and Data types
 Variable names must begin with a letter, under-
score or $, subsequent characters can be a letter,
under-score or $ or number.
 They can be assigned with proper value and used
where ever appropriate . They are called data
stores.
 To declare a variable:
 var x;
 var x=1; declare and initialize
 Variables declaration is not compulsory in
JavaScript. (If you don't declare a variable explicitly,
JavaScript will declare it implicitly for you.)
Example
<HTML><HEAD>
<SCRIPT type="text/javascript”>
$x=false;
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT type="text/javascript”>
alert($x==0);
</SCRIPT>
</BODY>
</HTML>
What do you
think the alert
box will
display?
Find what alert box will
display if $x is uninitialized?
OperatorsArithmetic:
+ - * / % += -= *= /= %= ++
--
Logical:
& | ! && ||
Relational:
> >= < <= == != === !==
String concatenation: +
Bit wise:
>> << >>> >>= <<= >>>=
Ternary: ?:
Conversion functions:
parseInt() and parseFloat()
To convert string to int and float respectively
Examples
S=“abc”;
I=123;
alert(S+I);
B=true;
alert(S+B);
alert(B+1);
alert(5+154e-2 +” kgs”);
alert(“Rs.” +5+154e-2 );
alert("34"==34);
alert(1<".23");
alert(10*"55");
 abc123
 abctrue
 124
6.54 kgs
Rs .51.54
 true
 550
Mixing up data types:
 NaN
 true
 false
 Infinity
 1
 0
 false
alert(10/"abc");
alert(1==true);
alert(1=="true");
alert(5/0);
alert(true & "3");
alert(true & "a");
alert(10/"abc"==NaN);
Then how do we compare
NaNs?
Use isNaN()
No we cannot,
because ‘a’ is a string.
We will look at the
methods of String and
there we will check
out if we can do the
achieve the same.
Shift operators
 The shift left operator looks at the integer to the left of the operator as a 32-bit binary
number.
 >> the SHIFT RIGHT operator
 << the SHIFT LEFT operator
 >>> the UNSIGNED SHIFT RIGHT operator
s=2;
t=-2;
alert((s<<2) +" "+(s>>2)+ " "+(s>>>2));
alert((t<<2)+" "+ (t>>2)+ " "+ (t>>>2));
Result of popup:
8 0 0
-8 -1 1073741823
Find what
happens when
you shift beyond
32 bits?
=== and !==
 alert("34"==34);
 Returns true
 alert("34"===34);
 Returns false
 === and !== are used for stricter comparisons
based on types.
 Note that alert(34.0===34) returns true
 alert(true===1); returns false
Control statements
 Same as in C++ or Java
 if else
 for
 for..in
 while
 do .. while
 switch
Getting input from user
 String prompt(question,defaultanswer)
 Example: prompt(“what is your
name”,””);
 A prompt box pops up. The user can enter some text
and click either "OK" or "Cancel" to proceed after
entering text.
 If the user clicks "OK" the box returns the input
value.
 If the user clicks "Cancel" the box returns null.
Function
 Like other programming languages, in JavaScript
also we can define a reusable code-block which
will execute when ever it is called.
 A function can be defined inside <head> ,
<body> or in a external file and can be called
from anywhere after it has been read.
 Syntax
function functionname(var1,var2,...,varX)
{
//some code
}
Example
<html><head>
<script type="text/javascript”>
<!--
function display(x){
alert(x);}
-->
</script>
</head>
<body>
<script>
<!--
display(“hello”);
-->
</script>
</body></html>
display()can be defined
anywhere even after the
function call
Calling a function
 The above function can be called as
 display();
 display(“hello”);
 Or display with any number of arguments
function display(x){
if(x==null)
x=“Greetings”;
alert(x) ;
}
 You can also pass values to a function that does not take
any arguments!
<body>
<script type="text/javascript”>
display("hello");
display();
function display(x)
{
if(x==null)
x="Greetings";
alert(x) ;
}
function display(){
alert("Greet") ;
}
</script>
</body>
Prints Greet for both the
calls
No overloading possible. If
overloaded functions are
provided, only the last defined
function is considered.
Local and Global variables
 All the variables that are not explicitly declared are global.
 Local variables are created using var inside the function
Global variable
Error.
<html><head>
<script>
total=0;
function sum(){
y=20;
var x=10;
total=x+y;
}
function display(){
sum();
alert(total);
alert(y);
alert(x);
}
Local variable
</script></head><body>
<script>
display();
</script>
</body></html>

Contenu connexe

Tendances

Basics java scripts
Basics java scriptsBasics java scripts
Basics java scripts
ch samaram
 
Java script final presentation
Java script final presentationJava script final presentation
Java script final presentation
Adhoura Academy
 

Tendances (20)

Basics java scripts
Basics java scriptsBasics java scripts
Basics java scripts
 
Java script
Java scriptJava script
Java script
 
Javascript
JavascriptJavascript
Javascript
 
Java script
Java scriptJava script
Java script
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Javascript tutorial
Javascript tutorialJavascript tutorial
Javascript tutorial
 
Javascript by geetanjali
Javascript by geetanjaliJavascript by geetanjali
Javascript by geetanjali
 
Java script -23jan2015
Java script -23jan2015Java script -23jan2015
Java script -23jan2015
 
Java Script
Java ScriptJava Script
Java Script
 
Java script final presentation
Java script final presentationJava script final presentation
Java script final presentation
 
Javascript
JavascriptJavascript
Javascript
 
Js ppt
Js pptJs ppt
Js ppt
 
JavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScriptJavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScript
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Java script Learn Easy
Java script Learn Easy Java script Learn Easy
Java script Learn Easy
 
Introduction to Java Script
Introduction to Java ScriptIntroduction to Java Script
Introduction to Java Script
 
Java script
Java scriptJava script
Java script
 
Basic JavaScript Tutorial
Basic JavaScript TutorialBasic JavaScript Tutorial
Basic JavaScript Tutorial
 
Learn javascript easy steps
Learn javascript easy stepsLearn javascript easy steps
Learn javascript easy steps
 

Similaire à 1. java script language fundamentals

Java script.pptx v
Java script.pptx                                     vJava script.pptx                                     v
Java script.pptx v
22x026
 
Jscript Fundamentals
Jscript FundamentalsJscript Fundamentals
Jscript Fundamentals
rspaike
 

Similaire à 1. java script language fundamentals (20)

Java scipt
Java sciptJava scipt
Java scipt
 
Unit 2.4
Unit 2.4Unit 2.4
Unit 2.4
 
Unit 2.4
Unit 2.4Unit 2.4
Unit 2.4
 
Introduction to java script
Introduction to java scriptIntroduction to java script
Introduction to java script
 
Web programming
Web programmingWeb programming
Web programming
 
Wt unit 2 ppts client sied technology
Wt unit 2 ppts client sied technologyWt unit 2 ppts client sied technology
Wt unit 2 ppts client sied technology
 
Wt unit 2 ppts client side technology
Wt unit 2 ppts client side technologyWt unit 2 ppts client side technology
Wt unit 2 ppts client side technology
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Introduction to javascript.ppt
Introduction to javascript.pptIntroduction to javascript.ppt
Introduction to javascript.ppt
 
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting
IT2255 Web Essentials - Unit III Client-Side Processing and ScriptingIT2255 Web Essentials - Unit III Client-Side Processing and Scripting
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting
 
FYBSC IT Web Programming Unit III Javascript
FYBSC IT Web Programming Unit III JavascriptFYBSC IT Web Programming Unit III Javascript
FYBSC IT Web Programming Unit III Javascript
 
2javascript web programming with JAVA script
2javascript web programming with JAVA script2javascript web programming with JAVA script
2javascript web programming with JAVA script
 
FSJavaScript.ppt
FSJavaScript.pptFSJavaScript.ppt
FSJavaScript.ppt
 
JavaScript Basics with baby steps
JavaScript Basics with baby stepsJavaScript Basics with baby steps
JavaScript Basics with baby steps
 
Java script.pptx v
Java script.pptx                                     vJava script.pptx                                     v
Java script.pptx v
 
Basics of Javascript
Basics of Javascript Basics of Javascript
Basics of Javascript
 
Jscript Fundamentals
Jscript FundamentalsJscript Fundamentals
Jscript Fundamentals
 
Javascript
JavascriptJavascript
Javascript
 
Introduction of javascript
Introduction of javascriptIntroduction of javascript
Introduction of javascript
 
JavaScript_III.pptx
JavaScript_III.pptxJavaScript_III.pptx
JavaScript_III.pptx
 

Plus de Rajiv Gupta

Introduction to jsf2
Introduction to jsf2Introduction to jsf2
Introduction to jsf2
Rajiv Gupta
 
Java Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4jJava Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4j
Rajiv Gupta
 
Core java 5 days workshop stuff
Core java 5 days workshop stuffCore java 5 days workshop stuff
Core java 5 days workshop stuff
Rajiv Gupta
 

Plus de Rajiv Gupta (18)

Spring5 hibernate5 security5 lab step by step
Spring5 hibernate5 security5 lab step by stepSpring5 hibernate5 security5 lab step by step
Spring5 hibernate5 security5 lab step by step
 
GOF Design pattern with java
GOF Design pattern with javaGOF Design pattern with java
GOF Design pattern with java
 
Introduction to jsf2
Introduction to jsf2Introduction to jsf2
Introduction to jsf2
 
Hibernate 3
Hibernate 3Hibernate 3
Hibernate 3
 
Weblogic 11g admin basic with screencast
Weblogic 11g admin basic with screencastWeblogic 11g admin basic with screencast
Weblogic 11g admin basic with screencast
 
Struts2
Struts2Struts2
Struts2
 
jsf2 Notes
jsf2 Notesjsf2 Notes
jsf2 Notes
 
Java 7
Java 7Java 7
Java 7
 
Struts2 notes
Struts2 notesStruts2 notes
Struts2 notes
 
Lab work servlets and jsp
Lab work servlets and jspLab work servlets and jsp
Lab work servlets and jsp
 
Java Servlet
Java Servlet Java Servlet
Java Servlet
 
Spring aop with aspect j
Spring aop with aspect jSpring aop with aspect j
Spring aop with aspect j
 
Spring 3.0 dependancy injection
Spring 3.0 dependancy injectionSpring 3.0 dependancy injection
Spring 3.0 dependancy injection
 
Java spring framework
Java spring frameworkJava spring framework
Java spring framework
 
Jsp Notes
Jsp NotesJsp Notes
Jsp Notes
 
Java Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4jJava Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4j
 
Advance C++notes
Advance C++notesAdvance C++notes
Advance C++notes
 
Core java 5 days workshop stuff
Core java 5 days workshop stuffCore java 5 days workshop stuff
Core java 5 days workshop stuff
 

Dernier

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
HenryBriggs2
 

Dernier (20)

Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 

1. java script language fundamentals

  • 2. JavaScript is Java code written in browsers! JavaScript can be used to do server side code as well JavaScript is programming language JavaScript make webpage look more beautiful! JavaScript are very slow and unpredictable! Myths or Facts Let us discover
  • 3. JavaScript  Is a scripting language developed by Netscape, and later standardized by W3C.  It was originally called LiveScript.  Used not only for web client and server side web programs, but also for iPhones, Adobe Photoshop, Adobe Flash action script to name a few.  JavaScript on web page works with HTML and CSS to create a DHTML page. Our focus
  • 4. Scripting and Programming language  Script are line of code that does not execute stand- alone. They run on browser (client-side) or application server (server-side) or on top of some other application.  They are interpreted at runtime and are not compiled.  Usually are loosely-typed language.  Examples: JavaScript, JScript, VBScript, PHP
  • 5. Is JScript not same as JavaScript? JScript is Microsoft version scripting language which is very similar to JavaScript Oh! I thought it was VBScript Microsoft produced 2 scripting languages- VBScript –(syntax similar to VB)and JScript
  • 6. JavaScript in a Web Page  To create more interactive pages- client side validations etc.  To generate html dynamically.  Event handling  To enhance browser capabilities by giving it a better look – printing on status bar etc.  Interaction with embedded components like applets and active x controls.
  • 7. Response Client Request HTML file JavaScri pt HTML files JavaScript Execution Server side code executes on the server and sends the response Script executes locally and interacts with the browser Web Server Server side code Other files So JavaScripts on client side executes faster than server-side code.
  • 8. Where will the code be written?  Email format validation  Password validation  Changing the colour of the page based on user’s input  Displaying different page based on the user’s role.  Refreshing just part of the page Client-side JavaScript code Server-side code
  • 9. Language Features  Syntax similar to C++ and Java  Case sensitive  Loosely typed  Interpreted  Platform independent  Object-based language  Semicolon, as separator for multiple statements in the same line.
  • 10. Object-based language  Also called prototype-based object oriented language  Object-based language  JavaScript objects are associative arrays  functions as object constructors and methods  prototypes instead of classes for inheritance. More on this later
  • 11. Simple Scripts in HTML <HTML><HEAD><TITLE>Hello</TITLE></HEAD> <BODY> First java script code<br> <SCRIPT type="text/javascript” > //Java script single line comment alert(“Hello java script”); /* java script script multi-line comment */ </SCRIPT> </BODY> </HTML> popup What will happen if you run this on a browser that does not support JavaScript?
  • 12. NOSCRIPT and Hiding scripts  Because some browsers don’t support JavaScript it is wise to put the JavaScript code inside HTML commented tags. <SCRIPT type="text/javascript” > <!— alert(“Hello java script”) --> </SCRIPT> <NOSCRIPT> Java script is not supported </NOSCRIPT> • The text inside the <NOSCRIPT> tag will not be processed by the JavaScript aware browsers.
  • 13. External Script  Scripts can also be written in a separate file and can be referenced in a HTML file. <HTML><HEAD><BODY> <SCRIPT type="text/javascript” SRC=“jsfile.js”> </SCRIPT> </BODY> </HTML> alert(“Hello”); jsfile.js Path can be 1. Absolute path http:://server1/get.jsp 2. Root relative: /scripts/jsfile.js 3. Document relative ../scripts/jsfile.js Why would you want to write JavaScript in another file?
  • 14. Placeholders for scripts within HTML  Inside head  only declarations. Declarations could for variables or functions.  No standalone executable statements must appear  Inside the body  any statements can appear  standalone executable statements inside the body are interpreted in place where it appears.  Along with the event handler  Script expression can be written as a value when an event like button click happens.
  • 15. Data types  Data types supported by Java Script are a) Numeric – integer and floating point numbers (64 bit, IEE754 floating point) alert(5+154e-2); b) String- alert(“Hello”) ;or alert(‘Hello’) ; c) Boolean- true, false
  • 16. alert(’God gives every bird it's food but he doesn't throw it into it's nest’); gives me error Hey! use escape sequences alert(’God gives every bird it's food but he doesn't throw it into it's nest’); Yes ! escape sequences can be used or you could also use double quote for string literal. alert(“God gives every bird it's food but he doesn't throw it into it's nest”);
  • 17. Variables and Data types  Variable names must begin with a letter, under- score or $, subsequent characters can be a letter, under-score or $ or number.  They can be assigned with proper value and used where ever appropriate . They are called data stores.  To declare a variable:  var x;  var x=1; declare and initialize  Variables declaration is not compulsory in JavaScript. (If you don't declare a variable explicitly, JavaScript will declare it implicitly for you.)
  • 19. OperatorsArithmetic: + - * / % += -= *= /= %= ++ -- Logical: & | ! && || Relational: > >= < <= == != === !== String concatenation: + Bit wise: >> << >>> >>= <<= >>>= Ternary: ?: Conversion functions: parseInt() and parseFloat() To convert string to int and float respectively
  • 20. Examples S=“abc”; I=123; alert(S+I); B=true; alert(S+B); alert(B+1); alert(5+154e-2 +” kgs”); alert(“Rs.” +5+154e-2 ); alert("34"==34); alert(1<".23"); alert(10*"55");  abc123  abctrue  124 6.54 kgs Rs .51.54  true  550 Mixing up data types:
  • 21.  NaN  true  false  Infinity  1  0  false alert(10/"abc"); alert(1==true); alert(1=="true"); alert(5/0); alert(true & "3"); alert(true & "a"); alert(10/"abc"==NaN); Then how do we compare NaNs? Use isNaN() No we cannot, because ‘a’ is a string. We will look at the methods of String and there we will check out if we can do the achieve the same.
  • 22. Shift operators  The shift left operator looks at the integer to the left of the operator as a 32-bit binary number.  >> the SHIFT RIGHT operator  << the SHIFT LEFT operator  >>> the UNSIGNED SHIFT RIGHT operator s=2; t=-2; alert((s<<2) +" "+(s>>2)+ " "+(s>>>2)); alert((t<<2)+" "+ (t>>2)+ " "+ (t>>>2)); Result of popup: 8 0 0 -8 -1 1073741823 Find what happens when you shift beyond 32 bits?
  • 23. === and !==  alert("34"==34);  Returns true  alert("34"===34);  Returns false  === and !== are used for stricter comparisons based on types.  Note that alert(34.0===34) returns true  alert(true===1); returns false
  • 24. Control statements  Same as in C++ or Java  if else  for  for..in  while  do .. while  switch
  • 25. Getting input from user  String prompt(question,defaultanswer)  Example: prompt(“what is your name”,””);  A prompt box pops up. The user can enter some text and click either "OK" or "Cancel" to proceed after entering text.  If the user clicks "OK" the box returns the input value.  If the user clicks "Cancel" the box returns null.
  • 26. Function  Like other programming languages, in JavaScript also we can define a reusable code-block which will execute when ever it is called.  A function can be defined inside <head> , <body> or in a external file and can be called from anywhere after it has been read.  Syntax function functionname(var1,var2,...,varX) { //some code }
  • 28. Calling a function  The above function can be called as  display();  display(“hello”);  Or display with any number of arguments function display(x){ if(x==null) x=“Greetings”; alert(x) ; }  You can also pass values to a function that does not take any arguments!
  • 29. <body> <script type="text/javascript”> display("hello"); display(); function display(x) { if(x==null) x="Greetings"; alert(x) ; } function display(){ alert("Greet") ; } </script> </body> Prints Greet for both the calls No overloading possible. If overloaded functions are provided, only the last defined function is considered.
  • 30. Local and Global variables  All the variables that are not explicitly declared are global.  Local variables are created using var inside the function Global variable Error. <html><head> <script> total=0; function sum(){ y=20; var x=10; total=x+y; } function display(){ sum(); alert(total); alert(y); alert(x); } Local variable