Example
<!DOCTYPE html>
<html>
<head>
<title>Comparison Operator</title>
</head>
<body>
<h3><u>Comparison Operator</u></h3>
<ul>
<li>
<script>
var a=2,b=2;
document.write(a==b);
</script>
</li>
<li>
<script>
var a=2,b='2';
document.write(a===b);
</script>
</li>
<li>
<script>
var a=2,b=5;
document.write(a!=b);
</script>
</li>
<li>
<script>
var a=8,b='6';
document.write(a!==b);
</script>
</li>
<li>
<script>
var a=8,b=6;
document.write(a>b);
</script>
</li>
<li>
<script>
var a=8,b=6;
document.write(a<b);
</script>
</li>
<li>
<script>
var a=8,b=6;
document.write(a>=b);
</script>
</li>
<li>
<script>
var a=8,b=6;
document.write(a<=b);
</script>
</li>
</body>
</html>
Conditional Statement in JavaScript
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.
Different types of Conditional Statements-
If statement
If…else statement
If....else...if statement
Switch statement
If Statement
This statement is used to execute statement only if a specified condition is true.
Example
<script>
var a=4;
if(a>0)
{
document.write("Positive number");
}
</script>
Ques: - A JavaScript program that displays “Good Morning!” if and only if time is less than 12 hours on web page otherwise page will remain blank.
Sol:-
<!DOCTYPE html>
<html>
<head>
<title>if statement</title>
</head>
<body>
<script>
if(new Date().getHours()<12)
{
document.write("Good Morning!");
}
</script>
</body>
</html>
If…statement
This statement is an extension of the simple if statement. It permits one of two statement or group of statement depending upon the logical test.
Example
<script>
var a=-7;
if(a>0)
{
document.write("Positive number");
}
else
{
document.write("Negative number")
}
</script>
Ques:- A JavaScript program that displays “Good Morning” if time is less than 12 hours otherwise it shows “Good Day” on the document.
Sol:-
<!DOCTYPE html>
<html>
<head>
<title>if...else statement</title>
</head>
<body>
<script>
if(new Date().getHours()<12)
{
document.write("Good Morning");
}
else
{
document.write("Good Day");
}
</script>
</body>
</html>
If…else…if statement
This statement is further an extension of the if…else…statement. This statement is useful for selecting one of many sets of statements to execute.
Example
<script>
var a=70;
if(a>0)
{
document.write("Positive Number");
}
else if(a<0)
{
document.write("Negative Number");
}
else
{
document.write("Number is Zero");
}
</script>
Ques:-Write a program to check whether a number input through prompt box is Zero, Odd or Even.
Sol:-
<!DOCTYPE html>
<html>
<head>
<title>if...else...if statement</title>
</head>
<body>
<script>
var n=prompt("Enter your number:","Type your number here");
if(n==0)
alert("The number is zero!");
else if (n%2)
alert("The number is odd!");
else
alert("The number is even!");
</script>
</body>
</html>
Switch Statement
A switch statement is used to execute different statement based on different conditions. It provides a better alternative than a long series of if…else if…statements.
Example
<script>
var num=3;
switch(num)
{
case 1:
document.write("One");
break;
case 2:
document.write("Two");
break;
case 3:
document.write("Three");
break;
default:
document.write("Number is invalid");
}
</script>
Ques:-To compute the day of week (in words) while you input the date within prompt dialog box.
Sol:-
<!DOCTYPE html>
<html>
<head>
<title>Switch statement</title>
</head>
<body>
<script>
var d=new Date(prompt("Enter your Date of Birth (e.g. May 28, 2000)","Month DD, YYYY"))
dy=d.getDay()
switch (dy)
{
case 0:
document.write("It was <b>Sunday</b> on that day.")
break
case 1:
document.write("It was <b>Monday</b> on that day.")
break
case 2:
document.write("It was <b>Tuesday</b> on that day.")
break
case 3:
document.write("It was <b>Wednesday</b> on that day.")
break
case 4:
document.write("It was <b>Thursday</b> on that day.")
break
case 5:
document.write("It was <b>Friday</b> on that day.")
break
case 6:
document.write("It was <b>Saturday</b> on that day.")
break
default:
document.write("Please input a valid Date in? prescribed format!!!")
}
</script>
</body>
</html>
JavaScript popup boxes
There are three kinds of popup boxes-
Alert box
Confirm box
Prompt box
Alert box
Alert( ) method of window object creates a small dialog box with a short text message and “OK” command button called alert box.
Example
<script>
alert(“hii guys”);
</script>
Confirm box
Confirm box is used if we want the user to verify and confirm the information. The user will have to click either “OK” or “Cancel” buttons.
Example
<script>
Confirm(“Are you agree to our conditions.”);
</script>
Prompt box
Prompt box allows getting input from the user. We can specify the default text for the text field. The information submitted by the user from prompt() can be stored in a variable.
Example
<script>
Prompt(“Enter your Password”);
</script>
Data Type
Every Variable has a data type that tells what kind of data is being stored in a variable.
There are two types of data types-
Primitive data type
Non-primitive data type
Primitive data type
The predefined data types provided by JavaScript language are known as primitive data types. Primitive data types are also known as in-built types.
Types of primitive data types
String
Number
Boolean
Undefined
String:- The string data type in JavaScript represents a sequence of characters that are surrounded by single or double quotes.
Example
<script>
var a=”Vimala Gurukul”;
document.write(a+”<br>”);
document.write(typeof a);
</script>
Number:- Number data type in javascript can be used to hold decimal values as well as values without decimals.
Example
<script>
var a=45;
document.write(a+”<br>”);
document.write(typeof a);
</script>
Boolean:- The Boolean data type can accept only two values i.e. true and false.
Example
<script>
var a=4;
var x=(a==4);
document.write(x+”<br>”);
document.write(typeof x);
</script>
Undefined:- The meaning of undefined is ‘value is not assigned’.
Example
<script>
var a;
var x=(a==3)
document.write(a+”<br>”);
document.write(typeof a);
</script>
Non-Primitive data type
The data types that are derived from primitive data types of the JavaScript language are known as non-primitive data types.
Types of non- primitive data types
Array
Object
Array:- An array is an object that can store multiple values at once
Example
<script>
var a=[4,6,7,8,”gurukul”];
document.write(a+”<br>”);
document.write(typeof a);
</script>
Object:-An object in JavaScript is an entity having properties and methods. Everything is an object in JavaScript.
Example
<script>
Var cap={color:”red”,price:500,company:”xyz”};
Document.write(cap.color+”<br>”);
Document.write(typeof cap);
</script>