Prepared by Muthulakshmi V
Prepared by Muthulakshmi V
8/2/2017 1
Outline
What is JavaScript?
Latest version of JavaScript
Programming with JavaScript
Declaring variables
Displaying messages using JavaScript
document.write
console.log
Data Types in JavaScript
The alert box
The prompt box – getting input from users
Adding Comments
Operators
8/2/2017 2
Outline
Conditionals
If
If else
switch
Looping
Arrays
Functions
Exception handling
Document Object Model
The DOM Hierarchy
DOM Methods
DOM Properties
AJAX 8/2/2017 3
What is JavaScript?
Is a programming language
JavaScript was not always known as JavaScript: the original name was
Mocha, a name chosen by Marc Andreessen, founder of Netscape
8/2/2017 4
Latest version of JavaScript
8/2/2017 5
Latest version of JavaScript
Year Version Description
1997 ECMAScript 1 First Edition
1998 ECMAScript 2 Editorial Changes
1999 ECMAScript 3 Regular Expressions , Exception
Handling
ECMAScript 4 Never Released
2009 ECMAScript 5 Adding “strict mode”, JSON
2011 ECMAScript 5.1 Editorial Changes
2015 ECMAScript 6 Classes, Modules, additional array
operators, collection objects
2016 ECMAScript 7 Added more array operators
8/2/2017 7
Including Script in HTML Pages
<head>
<script>
//In line scripts
</script>
</head>
<head>
<script src = ‘xyz.js’>
//External scripts
</script>
</head>
8/2/2017 8
Displaying Messages
To display messages in the browser we have the following 2 basic
methods.
document.write(message);
console.log(message);
This outputs the message in the console window. Usually used for
debugging purposes.
8/2/2017 9
Variables
Variables are containers that you can store values in.
var firstname;
Note:
You can name a variable nearly anything, but there are some name
restrictions
Note:
8/2/2017 11
Variables
After declaring a variable, you can give it a value.
firstname = ‘bob’;
You can do both these operations on the same line if you wish
You can retrieve the value by just calling the variable by name
firstname;
firstname = 'Steve';
8/2/2017 12
Variables – Data Types
Type Description Example
String A sequence of text known as string. var title = ‘JavaScript’
Enclosed with single or double
quotes.
unitialized variable
Variables – Data Types
The typeof operator is used to identify the type of the variable at any
point of time.
var myVariable;
myVariable = 5;
8/2/2017 14
Variables
Converting Variables to Numbers
var x = 50.55;
8/2/2017 15
Variables
Converting Variables to Numbers
variables to numbers:
Method Description
8/2/2017 16
Variables
Working with Dates
The Date object lets you work with dates (years, months, days,
hours, minutes, seconds, and milliseconds).
or as a number:
1501546504478
8/2/2017 17
Variables
Creating Date Objects
new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
8/2/2017 18
Variables
Date Methods
Method Description
getDate() Get the day as a number (1-31)
8/2/2017 19
The alert box
The alert box is used to display a popup message to the users.
(or)
8/2/2017 20
The prompt box
A prompt box is often used if you want the user to input a value
before entering a page.
When a prompt box pops up, the user will have to click either "OK" or
"Cancel" to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user
clicks "Cancel" the box returns null.
window.prompt(“message","defaultValue");
8/2/2017 21
Adding Comments
Comments are for the reference of the developers to understand the
code being written. In JS we have 2 ways to add comments.
/*
*/
8/2/2017 22
Operators
An operator is a valid symbol, which produces a new value based on a
value (or) values.
Arithmetic Operators
8/2/2017 23
Operators
Assignment Operators
= x=y x=y
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
8/2/2017 24
Operators
Comparison Operators
Operator Description
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
8/2/2017 25
? ternary operator
Operators
Logical Operators
Operator Description
|| logical or
! logical not
8/2/2017 26
Operators
JS Type Operators
Operator Description
8/2/2017 27
Conditionals
In any programming language, code needs to make decisions and
carry out actions accordingly depending on different inputs.
8/2/2017 28
Conditionals
if ... else statements – explanation
A set of curly braces, inside which we have some code — this can be any
code we like, and will only be run if the condition returns true.
Another set of curly braces, inside which we have some more code — this
can be any code we like, and will only be run if the condition is not true.
8/2/2017 29
Conditionals
if ... else statements – example
8/2/2017 30
Conditionals
if ... else statements – example
8/2/2017 31
Conditionals
switch statement
number of choices until they find one that matches that value, executing
8/2/2017 32
Conditionals
switch statement
8/2/2017 33
Conditionals
switch statement
8/2/2017 34
Looping
Loops can execute a block of code a number of times.
An exit condition, which is the criteria under which the loop stops —
usually the counter reaching a certain value.
8/2/2017 35
Looping
For loop
for (statement 1; statement 2; statement 3) {
code block to be executed
}
8/2/2017 36
Looping
while loop
8/2/2017 37
Arrays
JavaScript arrays are used to store multiple values in a single
variable.
An array is a special variable, which can hold more than one value at
a time.
Declaring arrays
Example
arrayname.length;
Example
8/2/2017 39
Arrays
Array Methods
pop()
Example
8/2/2017 40
Arrays
Array Methods
push()
8/2/2017 41
Arrays
Array Methods
shift()
Shifting is equivalent to popping, working on the first element
instead of the last.
The shift() method removes the first array element and "shifts" all
other elements to a lower index
8/2/2017 42
Arrays
Array Methods
unshift()
8/2/2017 43
Arrays
Array Methods
sort()
reverse()
fruits.sort();
fruits.reverse(); 8/2/2017 44
Functions
JavaScript functions are defined with the function keyword.
You can use a function declaration or a function expression.
function functionName(parameters) {
code to be executed
}
Example
function multiply(x,y){
return x * y;
8/2/2017 45
Functions
Function Expressions
without a name)
8/2/2017 46
Exception Handling
The try statement lets you test a block of code for errors.
The finally statement lets you execute code, after try and catch,
regardless of the result.
8/2/2017 47
DOCUMENT OBJECT MODEL - DOM
With the HTML DOM, JavaScript can access and change all the
8/2/2017 48
DOCUMENT OBJECT MODEL - DOM
8/2/2017 49
DOCUMENT OBJECT MODEL - DOM
8/2/2017 50
DOCUMENT OBJECT MODEL - DOM
The document object represents your web page.
If you want to access any element in an HTML page, you always start
with accessing the document object.
Finding HTML Elements
Method Description
document.getElementById(id) Find an element by
element id
document.getElementsByTagName(na Find elements by tag
me) name
8/2/2017 51
DOCUMENT OBJECT MODEL - DOM
Changing HTML Elements
Method Description
element.innerHTML = new html Change the inner HTML of an
content element
element.attribute = new value Change the attribute value of an
HTML element
element.setAttribute(attribute, Change the attribute value of an
value) HTML element
element.style.property = new style Change the style of an HTML
element
8/2/2017 52
DOCUMENT OBJECT MODEL - DOM
Adding and Removing Elements
Method Description
8/2/2017 53
DOCUMENT OBJECT MODEL - DOM
Adding Event Handlers
Method Description
8/2/2017 54
AJAX
Read data from a web server - after the page has loaded
8/2/2017 55
AJAX
8/2/2017 56
AJAX - Request
To send a request to a server, we use the open() and send() methods of
the XMLHttpRequest object.
Method Description
open(method, url, Specifies the type of request
async)
method: the type of request: GET or POST
url: the server (file) location
async: true (asynchronous) or false (synchronous)
send() Sends the request to the server (used for GET)
send(string) Sends the request to the server (used for POST)
8/2/2017 57
AJAX - Response
The onreadystatechange Property
The status property and the statusText property holds the status of
the XMLHttpRequest object
8/2/2017 58
AJAX - Response
The onreadystatechange Property
Property Description
8/2/2017 60
Exercise 1
Write a JavaScript that creates a different variable for each of the
members of your group.
8/2/2017 61
Exercise 2
Add to the JavaScript a prompt that asks the user their weight in pounds.
It uses a second prompt and variable to ask the user to enter their height
in inches.
It uses a third variable (not prompted) to hold the user’s bmi, calculated
as follows: (weight multiplied by 703), divided by (height times height).
8/2/2017 62
Exercise 3
Find an image of a rock, an image of paper, and an image of scissors.
Prompt the user to enter 0 for rock, 1 for paper, and 2 for scissors.
ii. If the user entered 1, use document.write to write out the html to
display the image of paper
iii. If the user entered 2, use document.write to write out the html to
display the image of scissors.
8/2/2017 63
Exercise 4
Loan Calculator
8/2/2017 64
Exercise 5
Contacts Search
8/2/2017 65