Principles of Web Development: Javascript and HTML
Principles of Web Development: Javascript and HTML
Document on
DOCUMENTED BY:-
1|Page
CONTENTS
S NO. TOPIC
1. Section A 5-15
2|Page
CERTIFICATE
3|Page
ACKNOWLEDGEMENT
4|Page
1. SECTION A
1.1WHAT IS JAVASCRIPT?
A literal is the actual number or piece of text or boolean value that the
variable represents.
5|Page
An object is a collection of variables held together by a parent variable, or a
document component.
The next most important part of a script is an operator. Operators assign
literal values to variables or say what type of tests to perform.
The next most important part of a script is a control structure. Control
structures say what scripts should be run if a test is satisfied.
Example-
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
<p id="demo"></p>
</body>
</html>
Output
6|Page
1.2.Difference Between JavaScript and HTML
7|Page
1.3. JavaScript Data Types
There are majorly two types of languages. First, one is Statically typed
language where each variable and expression type is already known at compile
time. Once a variable is declared to be of a certain data type, it cannot hold values
of other data types. .Example: C, C++, Java.
In JavaScript there are two different kinds of data: primitives, and objects. A primitive is simply a
data type that is not an object, and has no methods.
Boolean
Number
String
Null
Undefined
Symbol
Boolean
A boolean represents only one of two values: true, or false. Think of a boolean as
an on/off or a yes/no switch.
var boo1 = true;
var boo2 = false;
8|Page
Number
There is only one type of Number in JavaScript. Numbers can be written with or
without a decimal point. A number can also be +Infinity, -Infinity, and NaN (not a
number).
var num1 = 32;
var num2 = +Infinity;
String
Strings are used for storing text. Strings must be inside of either double or single
quotes. In JS, Strings are immutable (they cannot be changed).
var str1 = 'hello, it is me';
var str2 = "hello, it's me";
Null
Undefined
Symbol
Objects?
Objects are not a primitive data Type.
9|Page
An object is a collection of properties. These properties are stored in key/value
pairs. Properties can reference any type of data, including objects and/or primitive
values.
For example:
<script>
var person = {
name: "Peter",
age: 28,
gender: "Male",
displayName: function() {
alert(this.name);
}
};
10 | P a g e
1.4. JavaScript Looping
Looping
Very often when you write code, you want the same block of code to run a number
of times. You can use looping statements in your code to do this.
1.For loop
The 'for' loop is the most compact form of looping. It includes the following three
important parts −
The loop initialization where we initialize our counter to a starting value.
The initialization statement is executed before the loop begins.
The test statement which will test if a given condition is true or not. If the
condition is true, then the code given inside the loop will be executed,
otherwise the control will come out of the loop.
The iteration statement where you can increase or decrease your counter.
Note-You can put all the three parts in a single line separated by
semicolons.
Syntax-
for (initialization; condition; iteration) {
Example-
11 | P a g e
for (i = 0; i < 5; i++) {
text += "The number is " + i + "<br>";
}
Output-
JavaScript For Loop
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The while loop loops through a block of code as long as a specified condition is
true.
Syntax
while (condition) {
// code block to be executed
}
Example
In the following example, the code in the loop will run, over and over again, as
long as a variable (i) is less than 10:
Example-
while (i < 10) {
text += "The number is " + i;
i++;
}
12 | P a g e
Output-
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
2. do…while loop
Syntax:
do
} while (condition)
The do…while loop is very similar to while loop. The only difference is that in
do…while loop, the block of code gets executed once even before checking the
condition.
Example-
13 | P a g e
var text = "";
var i = 0;
do {
text += "The number is " + i;
i++;
}
while (i < 5);
Output-
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
Conditional statements are used to decide the flow of execution based on different
conditions. If a condition is true, you can perform one action and if the condition is
false, you can perform another action.
The if/else statement executes a block of code if a specified condition is true. If the
condition is false, another block of code can be executed.
Use else to specify a block of code to be executed, if the same condition is false
14 | P a g e
Use else if to specify a new condition to test, if the first condition is false
If statement
The if Statement
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
If…Else statement
Syntax:
if (condition)
else
15 | P a g e
we can use If….Else statement if we have to check two conditions and execute a
different set of codes.
Syntax:
if (condition1)
else if(condition2)
else
we can use If….Else If….Else statement if we want to check more than two
conditions.
16 | P a g e
2.Section B
2.1 Write a program in JavaScript to following arithmetic Operators.
<!doctype html>
<html>
<head>
<CENTER><BOLD> <h1><font color = "Black"> JAVASCRIPT
PROGRAM </font></h1> </BOLD></CENTER>
</BR></BR></BR></BR>
<script>
function add()
{
var a,b,c;
a=Number(document.getElementById("first").value);
b=Number(document.getElementById("second").value);
c=a+b;
document.getElementById("answer1").value=c;
}
function sub()
{
var a,b,c;
a=Number(document.getElementById("1st").value);
b=Number(document.getElementById("2nd").value);
c=a-b;
document.getElementById("answer2").value=c;
}
function multiply()
{
var a,b,c;
a=Number(document.getElementById("1").value);
b=Number(document.getElementById("2").value);
c=a*b;
document.getElementById("answer3").value=c;
}
17 | P a g e
function divide()
{
var a,b,c;
a=Number(document.getElementById("one").value);
b=Number(document.getElementById("two").value);
c=a/b;
document.getElementById("answer4").value=c;
}
</script>
</head>
<body>
<body bgcolor = "Salmon">
<font color = "#cc66ff"><bold> 1.ADD </br> </font>
Enter the first number: <input id="first">
Enter the second number: <input id="second">
<button onclick="add()"> Addition </button>
<input id="answer1">
<br> <br>
<font color = "Yellow"> 2.SUBTRACT </br> </font>
Enter the first number: <input id="1st">
Enter the second number: <input id="2nd">
<button onclick="sub()"> Subtraction </button>
<input id="answer2">
<br><br>
<font color = " Red"> 3.MULTIPLY </br> </font>
Enter the first number: <input id="1">
Enter the second number: <input id="2">
<button onclick="multiply()"> Product </button>
<input id="answer3">
<br><br>
<font color = " Violet"> 4.DIVIDE </br> </font>
Enter the first number: <input id="one">
Enter the second number: <input id="two">
<button onclick="divide()"> Division </button>
18 | P a g e
<input id="answer4">
<br></br></br><br>
</body>
</html>
<html>
<head>
<script>
function odd_even(){
var no;
no=Number(document.getElementById("no_input").value);
if(no%2==0)
alert("Even Number");
else
alert("Odd Number");
}
19 | P a g e
</script>
</head>
<body>
</body>
</html>
OUTPUT-
Click Me
function largest(a, b) {
if(a > b)
return a;
return 0;
20 | P a g e
else
return b;
console.log(largest(first, second));
Output
5
2.4 Differentiate between for loop, while loop, do..while loop giving
suitable examples-
22 | P a g e
REFERENCES:-
https://www.guru99.com/introduction-to-javascript.html
https://www.geeksforgeeks.org/difference-between-while-and-do-while-loop-in-c-c-java/
https://www.w3resource.com/javascript/operators/arithmetic-addition-subtraction-
multiplication-division.php
https://www.educba.com/html-vs-javascript/
23 | P a g e