Unit Iv Javascript - Web Technology (I Semester)
Unit Iv Javascript - Web Technology (I Semester)
Unit Iv Javascript - Web Technology (I Semester)
SECTION – A (3 MARKS)
1. Interpreted languages
2. Easy to learn
3. Easy to Debug and Test
4. Event-Based Programming
5. Procedural Capabilities
6. Platform Independence
Advantages of JavaScript
There are lot of advantages of using JavaScript in Web Technologies.
2. Easy to learn: The syntax of JavaScript is very easy. Any person can learn it very easily
and use it to develop dynamic and attractive websites.
3. Easy to Debug and Test: JavaScript code is interpreted line by line. The errors are
indicated along with line number. It is very easy to find error in the code, correct it and test
it gain.
<scripttype="text/javascript">
var money;
var name;
</script>
Example: in x + 2 = 6, x is the variable.
Syntax
isNan(0);
isNan('isNan');
isNan(-1.895);
Since 2 and 5 are integers they, will be added numerically. And since 3 is a
string its concatenation will be done. so the result would be 73
The prompt dialog box is very useful when user wants to get input. Thus, it enables the user to
interact. The user needs to fill in the field and then click OK.
This dialog box takes two parameters: (i) a label which you want to display in the text box and
(ii) a default string to display in the text box.
This dialog box has two buttons: OK and Cancel. If the user clicks the OK button, the window
method prompt() will return the entered value from the text box. If the user clicks the Cancel
button, the window method prompt()returns null.
Syntax:
Prompt(“message”, “default value”);
example :
prompt(“Enter your name : “, “abc”);
JavaScript is a dynamic type language, it means no need to specify type of the variable
because it is dynamically used by JavaScript engine. Just use var here to specify the data
type. It can hold any type of values such as numbers, strings etc.
For example:
var a=40;//holding number
var b="Rahul";//holding string
Syntax
The syntax of while loop in JavaScript is as follows −
while (expression)
{
Statement(s) to be executed if expression is true
}
SECTION – B (8 MARKS)
The JavaScript for loop iterates the elements for the fixed number of times.
The syntax of for loop is given below.
code to be executed
Example:
<html>
<body>
<script>
var count;
document.write("Starting Loop" + "<br />");
for(count = 0; count < 10; count++)
{
document.write("Current Count : " + count );
document.write("<br />");
}
document.write("Loop stopped!");
</script></body></html>
Output:
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Loop stopped!
Flow Chart
The following flow chart explains a switch-case statement works.
Syntax
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. If nothing matches,
a default condition will be used.
switch(expression)
break;
break;
...
break;
default: statement(s)
Number object:
The JavaScript number object enables to represent a numeric value. It may be integer or
floating-point.By the help of Number() constructor, it can create number object in
JavaScript. For example: var n=new Number(value);
Keywords
Keywords are reserved words in JavaScript which cannot be used to name the variables labels
or function names. Here are a total of 63 keywords which JavaScript provides the
programmers.Here some of them are shown in the below-mentioned diagram.
JavaScript Reserved Keywords List
abstract, byte, const, delete, eval, float, arguments, case, continue, do, false, for, char,final
Identifiers
Identifiers are names.
In JavaScript, identifiers are used to name variables (and keywords, and
functions, and labels).
The rules for legal names are much the same in most programming languages.
In JavaScript, the first character must be a letter, or an underscore (_), or a
dollar sign ($).
Subsequent characters may be letters, digits, underscores, or dollar signs.
Numbers are not allowed as the first character.
All JavaScript identifiers are case sensitive.
eg:
var lastname, lastName;
lastName = "Doe";
lastname = "Peterson";
It displays a dialog box with two buttons: OK and Cancel.If the user clicks on the OK
button, the window method confirm() will return true. If the user clicks on the Cancel
button, then confirm() returns false.
<html>
<head>
<script>
function display()
{
confirm("Do you want to EXIT?")
}
</script>
</head>
<body>
<input type="button" onclick="display()" value="confirm" />
</body>
</html>
Output
String Length
To find the length of a string, use the built-in length property:
Example
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;
Conditional Statements
In JavaScript we have the following conditional statements:
Use if to specify a block of code to be executed, if a specified condition is true
Use else to specify a block of code to be executed, if the same condition is false
Use else if to specify a new condition to test, if the first condition is false
Use switch to specify many alternative blocks of code to be executed
The if Statement
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate a JavaScript error.
Example
Make a "Good day" greeting if the hour is less than 18:00:
if (hour < 18) {
greeting = "Good day";
}
The result of greeting will be:
Good day
The else Statement
Use the else statement to specify a block of code to be executed if the condition is false.
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example
If the hour is less than 18, create a "Good day" greeting, otherwise "Good evening":
if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
The result of greeting will be:
Good day
The else if Statement
Use the else if statement to specify a new condition if the first condition is false.
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Example
If time is less than 10:00, create a "Good morning" greeting, if not, but time is less than 20:00,
create a "Good day" greeting, otherwise a "Good evening":
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
The result of greeting will be:
Good day
Output:
The prompt dialog box is very useful to get user input. Thus, it enables to interact with the
user. The user needs to fill in the field and then click OK.
This dialog box takes two parameters: (i) a label (ii) a default string to display in the text box.
This dialog box has two buttons: OK and Cancel. If the user clicks the OK button, the window
method prompt() will return the entered value from the text box. If the user clicks the Cancel
button, the window method prompt()returns null.
eg:
<html>
<head>
<script>
functionprompt_fun()
{
varfname=prompt("Please enter your name:","Your name");
document.write("Greetings " + fname);
}
</script>
</head>
<body>
<br><br>
<input type="button" onClick="prompt_fun()" value="prompt box" />
</body>
</html>
OUTPUT
Confirm Dialog Box:
It displays a dialog box with two buttons: OK and Cancel.If the user clicks on the OK button, the window
method confirm() will return true. If the user clicks on the Cancel button, then confirm() returns false.
<html>
<head>
<script>
function display()
{
confirm("Do you want to EXIT?")
}
</script>
</head>
<body>
<input type="button" onclick="display()" value="confirm" />
</body>
</html>
Output
5. Explain on How to use DOM and Events?
JAVASCRIPT EVENTS
The change in the state of an object is known as an Event. In html, there are various events
which represents that some activity is performed by the user or by the browser. When
javascript code is included in HTML, js react over these events and allow the execution. This
process of reacting over the events is called Event Handling. Thus, js handles the HTML events
via Event Handlers.
For example, when a user clicks over the browser, add js code, which will execute the task to be
performed on the event.
Some of the HTML events and their event handlers are:
Mouse events:
Event Performed Event Handler Description
click onclick When mouse click on an element
mouseover onmouseover When the cursor of the mouse comes over the element
mouseout onmouseout When the cursor of the mouse leaves an element
mousedown onmousedown When the mouse button is pressed over the element
mouseup onmouseup When the mouse button is released over the element
mousemove onmousemove When the mouse movement takes place.
Keyboard events:
Event Performed Event Handler Description
Keydown & Keyup onkeydown & onkeyup When the user press and then release the key
Form events:
Event Performed Event Handler Description
focus onfocus When the user focuses on an element
submit onsubmit When the user submits the form
blur onblur When the focus is away from a form element
change onchange When the user modifies or changes the value of
a form element
Window/Document events
Event Performed Event Handler Description
Load onload When the browser finishes the loading of the page
unload onunload When the visitor leaves the current webpage, the
browser unloads it
resize onresize When the visitor resizes the window of the
browser