Java Script
Java Script
WEB
ENGINEERING
PPT – 1
1|Page
NotesHub.co.in | Download Android App
JavaScript
Advantages of JavaScript
Increased interactivity
Richer interfaces
2|Page
NotesHub.co.in | Download Android App
Limitations
Client-side JavaScript does not allow the reading or writing of files. This has been kept
for security reason.
JavaScript cannot be used for networking applications because there is no such support
available.
o Language - specifies what scripting language you are using. Typically, its value will be
javascript. Although recent versions of HTML (and XHTML, its successor) have phased
out the use of this attribute.
o Type − This attribute is what is now recommended to indicate the scripting language
in use and its value should be set to "text/javascript".
3|Page
NotesHub.co.in | Download Android App
Here document.write() is a function which is used to write a String in HTML Web page.
JavaScript ignores spaces, tabs, and newlines that appear in JavaScript programs
JavaScript, however, allows you to omit this semicolon if each of your statements are placed
on a separate line, But when formatted in a single line as follows, you must use semicolons.
If you want to have a script run on some event, such as when a user clicks somewhere, then you
will place that script in the head.
4|Page
NotesHub.co.in | Download Android App
Example
Output
5|Page
NotesHub.co.in | Download Android App
If you need a script to run as the page loads so that the script generates content in the page, then
the script goes in the <body> portion of the document. In this case, you would not have any
function defined using JavaScript.
Example
Output
6|Page
NotesHub.co.in | Download Android App
Example
7|Page
NotesHub.co.in | Download Android App
Create a JavaScript File and save it with “.js” extension and then include the file in a HTML web
page using <script tag and src attribute as shown below:
Js-5.js
Js-6.html
8|Page
NotesHub.co.in | Download Android App
JavaScript also defines two trivial data types, null and undefined, each of which defines only a
single value. In addition to these primitive data types, JavaScript supports a composite data type
known as object.
JavaScript Variables
Variables are declared with the var keyword, eg:
JavaScript is untyped language. This means that a JavaScript variable can hold a value of
any data type.
9|Page
NotesHub.co.in | Download Android App
Unlike many other languages, you don't have to tell JavaScript during variable declaration
what type of value the variable will hold.
The value type of a variable can change during the execution of a program and JavaScript
takes care of it automatically.
JavaScript Operators
Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
Arithmetic Operators
+ (Addition)
- (Subtraction)
(Multiplication)
/ (Division)
% (Modulus)
++ (Increment)
-- (Decrement)
10 | P a g e
NotesHub.co.in | Download Android App
Comparison Operators
= = (Equal)
!= (Not Equal)
> (Greater than)
< (Less than)
>= (Greater than or Equal to)
<= (Less than or Equal to)
11 | P a g e
NotesHub.co.in | Download Android App
Logical Operators
13 | P a g e
NotesHub.co.in | Download Android App
Example
Output:
If-else statement
JavaScript supports conditional statements which are used to perform different actions based on
different conditions.
14 | P a g e
NotesHub.co.in | Download Android App
if statement
if...else statement
if Statement
if(expression)
{
Statements to execute if expression is true
}
15 | P a g e
NotesHub.co.in | Download Android App
if…else statement
if(expression)
{
Statements to execute if expression is true
}
else
{
Statements to execute
}
16 | P a g e
NotesHub.co.in | Download Android App
17 | P a g e
NotesHub.co.in | Download Android App
18 | P a g e
NotesHub.co.in | Download Android App
19 | P a g e
NotesHub.co.in | Download Android App
The objective of a switch statement is to give an expression to evaluate and several different
statements to execute based on the value of the expression.
The interpreter checks each case against the value of the expression until a match is found.
If nothing matches, a default condition will be used.
20 | P a g e
NotesHub.co.in | Download Android App
While Loops
The purpose of a while loop is to execute a statement or code block repeatedly as long as
an expression is true. Once the expression becomes false, the loop terminates.
21 | P a g e
NotesHub.co.in | Download Android App
Output
22 | P a g e
NotesHub.co.in | Download Android App
Do…while Loop
The do...while loop is similar to the while loop except that the condition check happens at the
end of the loop. This means that the loop will always be executed at least once, even if the
condition is false.
23 | P a g e
NotesHub.co.in | Download Android App
For Loop
For…in Loop
The for...in loop is used to loop through an object's properties.
In each iteration, one property from object is assigned to variable-name and this loop
continues till all the properties of the object are exhausted.
24 | P a g e
NotesHub.co.in | Download Android App
Output
25 | P a g e
NotesHub.co.in | Download Android App
26 | P a g e
NotesHub.co.in | Download Android App
Output
Functions in JavaScript
A function is a group of reusable code which can be called anywhere in your program. This
eliminates the need of writing the same code again and again.
It helps programmers in writing modular codes. Functions allow a programmer to divide a
big program into a number of small and manageable functions.
27 | P a g e
NotesHub.co.in | Download Android App
Function Definition
Function Keyword is used to define the function followed by unique function name, a list of
parameters(might be empty)
Example
28 | P a g e
NotesHub.co.in | Download Android App
Function Parameters
A function can take multiple parameters separated by comma
Example
29 | P a g e
NotesHub.co.in | Download Android App
Return Statement
30 | P a g e
NotesHub.co.in | Download Android App
31 | P a g e
NotesHub.co.in | Download Android App
JavaScript Events
JavaScript's interaction with HTML is handled through events that occur when the user or
the browser manipulates a page.
When the page loads, it is called an event. When the user clicks a button, that click too is
an event. Other examples include events like pressing any key, closing a window, resizing
a window, etc.
32 | P a g e
NotesHub.co.in | Download Android App
33 | P a g e
NotesHub.co.in | Download Android App
34 | P a g e
NotesHub.co.in | Download Android App
Syntax Errors
Runtime Errors
Logical Errors
Try..catch..finally statement
The try block must be followed by either exactly one catch block or one finally block (or
one of both)
Example of Try-Catch
35 | P a g e
NotesHub.co.in | Download Android App
36 | P a g e
NotesHub.co.in | Download Android App
Example of Try-Catch-Finally
37 | P a g e
NotesHub.co.in | Download Android App
38 | P a g e
NotesHub.co.in | Download Android App
39 | P a g e
NotesHub.co.in | Download Android App
JavaScript Dates
Output
40 | P a g e
NotesHub.co.in | Download Android App
41 | P a g e
NotesHub.co.in | Download Android App
Set Methods
Parsing Dates
42 | P a g e
NotesHub.co.in | Download Android App
Create a JavaScript Program to display current date and time. On the HTML Page display buttons
such as “getDay” etc. and on click of each button show the answer in H1 heading.
Note: Use at least 10 buttons for different functions.
JavaScript Arrays
Example
43 | P a g e
NotesHub.co.in | Download Android App
44 | P a g e
NotesHub.co.in | Download Android App
45 | P a g e