Javascript
– It is a client- and server-side object-based scripting
language that is used to make interactive Web pages
– Itis the most commonly used scripting language to add
dynamism and interactivity to Web pages
– JavaScript, written on the client-side, executes on a client
browser, thereby reducing the load on the server
Javascript can be put
Inside the <head>
* Inside the <body>
* Using external javascript file
<script src=“even.js”></script>
Data types
– Undefined
var foo;
alert(foo); // This will open a dialog containing “undefined”.
– Null
var foo = null;
alert(foo); // This will open a dialog containing “null”.
– Numbers
* var foo = 5;
* alert(foo); // This will open a dialog containing “5”.
– Booleans (Ex: true, false)
* var foo = true;– Strings (Ex: “Raj”, “Sourya”)
– Array
* var foo = [5, “five”, “5”];
* alert( foo[0] ); // Alerts “5“
alert( foo[1] ); // Alerts “five”
alert( foo[2] ); // Also alerts “5”
==
Is equal to
!=
Is not equal to
===
Is identical to (equal to and of the same
data type)
!==
Is not identical to
>
Is greater than
> =
Is greater than or equal to
<
Is less than
<=
Is less than or equal to
Equal versus identical
– Example
* alert( “5” == 5 ); // This will alert “true”. They’re both
“5”.
* alert( “5” === 5 ); // This will alert “false”. They’re
both “5”, but they’re not the same data type.
* alert( “5” !== 5 ); // This will alert “true”, since they’re
not the same data type
Control flow statements
– Selection statements (if, if-else, switch)
– Loops (for, while, do-while)
– Jump statements (break, continue)
Functions
Popup Boxes
– A popup box is a window that displays a message along with an OK
button
– It may also contain a Cancel button
– It can prompt users to enter some text
JavaScript supports three types of popup boxes:
– The alert box
* Generally used to display an alert message while executing the JavaScript
code
* Used to display error messages after we validate a form
– The confirm box
* Advanced form of the alert box
* Displays a dialog box with two buttons, OK and Cancel
– The prompt box
* Used to input a value from a user
* It contains a text box and OK and Cancel buttons
Events
Types of event handler:
– onclick handler triggers a script when the user
clicks.
– the onload event handler triggers a script when
the document is loaded.
– onmouseover handler triggers a script when the
user mouses over an element.
There are three common methods for applying
event handlers to items within our pages:
– As an HTML attribute (we may use it)
– As a method attached to the element (we may use
it)
– Using addEventListener
* In latter two approaches, we’ll use the window object.