Decision/Conditional(if-else)Statement Examples
Example : A JS program to print Larger Values from two values.
<html>
<head>
</head>
<body>
<script>
x=20;
y=30;
if(x>y)
{
document.write("First value is Larger");
}
if(x<y)
{
document.write("Second value is Larger");
}
if(x==y)
{
document.write("Both values are equal");
}
</script>
</body>
</html>
Output:
Second value is Larger
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
x=20;
y=30;
if(x>y)
{
document.write(x +" is Larger");
}
if(x<y)
{
document.write(y +" is Larger");
}
if(x==y)
{
document.write("Both values are equal");
}
</script>
</body>
</html>
Output:
30 is Larger
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
var x=prompt("Enter first value", "Enter Numeric Value");
var y=prompt("Enter second value", "Enter Numeric Value");
if(x>y)
{
document.write("First value " + x + " is Larger");
}
if(x<y)
{
document.write("Second value " + y + " is Larger");
}
if(x==y)
{
document.write("Both values are equal");
}
</script>
</body>
</html>
Output:
20
30
Second value 30 is Larger
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
let x = 10, y = 20, z = 15;
document.write("Largest value is : ", Math.max(x, y, z));
</script>
</body>
</html>
Output:
Largest value is : 20
Example : A JS program to check whether the no. is Even or Odd.
<html>
<head>
</head>
<body>
<script>
x=20;
if(x%2==0)
{
document.write(x +" is Even no.");
}
else
{
document.write(x +" is Odd no.");
}
</script>
</body>
</html>
Output:
20 is Even no.
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
var x=prompt("Enter a tble value");
if(x%2==0)
{
document.write(x +" is Even no.");
}
else
{
document.write(x +" is Odd no.");
}
</script>
</body>
</html>
Output:
41 is Odd no.
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
x=20;
document.write(x % 2 === 0 ? "No. is Even" : "No. is Odd");
</script>
</body>
</html>
Output :
No. is Even
Example : A JS program to Print the Grades of Students on an Exam.
<html>
<head>
<h4>Table values Program</H4>
</head>
<body>
<script>
var totmarks=prompt("Enter total marks of students");
if(totmarks>=800 && totmarks<=900)
{
document.write("Super Grade");
}
else if(totmarks>=600 && totmarks<800)
{
document.write("A++ Grade");
}
else if(totmarks>=400 && totmarks<600)
{
document.write("B Grade");
}
else
{
document.write("<b>Not Satisfactory</b>");
}
</script>
</body>
</html>
Example : A JS program to check if the Year is Leap or Not.
<html>
<head>
</head>
<body>
<script>
year=2025;
if((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0)
document.write("Leap Year")
else
document.write("Not Leap Year");
</script>
</body>
</html>
Output:
Not Leap Year
Example : A JS program to print the week’s name when entering values from 1 to 7 using Switch Case Statement.
<html>
<head>
<h4>Switch case Program</H4>
</head>
<body>
<script>
var choice=parseInt(prompt("Enter your choice between 1-7 to see name of week"));
switch(choice)
{
case 1:
{
document.write("Sunday");
break;
}
case 2:
{
document.write("Monday");
break;
}
case 3:
{
document.write("Tuesday");
break;
}
case 4:
{
document.write("<b>Wednesday</b>");
break;
}
case 5:
{
document.write("<b>Thursday</b>");
break;
}
case 6:
{
document.write("<b>Friday</b>");
break;
}
case 7:
{
document.write("<b>Saturday</b>");
break;
}
default:
{
document.write("<b>Invalid Entered Choice</b>");
}
}
</script>
</body>
</html>
Example : A JS program to print the output of Simple Calculator using Switch Case statement.
<!DOCTYPE html>
<html>
<head>
<title>Simple Calculator</title>
<script>
function calculate()
{
var num1=parseInt(prompt("User Prompt Instruction for Simple Calculator",'Enter first numeric value'));
var num2 = parseInt(prompt("User Prompt Instruction for Simple Calculator",'Enter Second numeric value'));
var opt = prompt("User Prompt Instruction for Simple Calculator","Now Enter the Operator symbol such as +,-,*,/,%",'User Prompt Instruction');
var result;
switch(opt)
{
case '+':
{
result = num1 + num2;
break;
}
case '-':
{
result = num1 - num2;
break;
}
case '*':
{
result = num1 * num2;
break;
}
case '/':
{
result = num1 / num2;
break;
}
case '%':
{
result = num1 % num2;
break;
}
default:
{
document.write("<b>Invalid/Wrong Choice</b>");
}
}
alert("The result is = " + result);
}
</script>
</head>
<body>
<h1>Simple Calculator Using JS</h1>
<form>
<td><button onclick="calculate()">Click for Simple Calculator</button></td>
</form>
</body>
</html>
Looping Statement Examples
Example : A JS program to print all the values from 50 to 60.
<html>
<head>
<h4>Loop values Program</H4>
</head>
<body>
<script>
for(var i=50; i<=60; i=i+1)
{
document.write(i+" ");
//document.write(i+"</br>");
}
</script>
</body>
</html>
Example : A JS program to print all the values of a table given by the user using for, while, and do-while loop.
<html>
<head>
<h4>Table values Program</H4>
</head>
<body>
<script>
var x=prompt("Enter a table value");
for(var i=1; i<=10; i=i+1)
{
document.write((i*x)+"</br>");
}
</script>
</body>
</html>
-------------------- OR --------------------
<html>
<head>
<h4>Table values Program</H4>
</head>
<body>
<script>
var x=prompt("Enter a table value");
var i=1;
while(i<=10)
{
document.write((x*i)+"</br>");
i=i+1;
}
</script>
</body>
</html>
-------------------- OR --------------------
<html>
<head>
<h4>Table values Program</H4>
</head>
<body>
<script>
var x=prompt("Enter a table value");
var i=1;
do
{
document.write((x*i)+"</br>");
i=i+1;
}while(i<=10);
</script>
</body>
</html>
Example : A JS program to Check whether the Value is ArmStrong or Not.
<html>
<body>
<script>
let sum = 0;
num=153;
temp = num;
let digits = num.toString().length;
while (temp > 0)
{
let digit = temp % 10;
sum = sum + Math.pow(digit, digits);
temp = Math.floor(temp / 10);
}
if (sum === num)
document.write("No. is Armstrong No.");
else
document.write("No. is Not Armstrong No.");
</script>
</body>
</html>
Output:
No. is Armstrong No.
Example : A JS program to print all the values from an array using a for-in looping statement.
<html>
<body>
<script>
var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya", "Guava", "PineApple", "Pomegranate"];
for(var i in fruits)
{
document.write(fruits[i] + "</br>");
}
</script>
</body>
</html>
Output :
Apple
Banana
Mango
Orange
Papaya
Guava
PineApple
Pomegranate
Example : A JS program to display the output using the concept of With Statement.
<html>
<head>
<script>
function addPrice(amount)
{
with(this)
{
price = amount;
}
}
function book_details(title, author)
{
this.title = title;
this.author = author;
this.price = 0;
this.addPrice = addPrice; // Assign addPrice method as property.
}
</script>
</head>
<body>
<script>
var ObjBook = new book_details("Mahabharat", "VedVyas");
ObjBook.addPrice(750);
document.write("The holy book title is : " + ObjBook.title + "<br>");
document.write("The holy book author is : " + ObjBook.author + "<br>");
document.write("The holy book price is : " + ObjBook.price + "<br>");
</script>
</body>
</html>
Output:
The holy book title is : Mahabharat
The holy book author is : VedVyas
The holy book price is : 750
Example : A JS program to print all the values except a few values from a range of values given by the user using the Continue statement.
<html>
<head>
<h4>Loop values Program</H4>
</head>
<body>
<script>
for(var i=50; i<=60; i=i+1)
{
if(i==54 || i==56)
{
continue;
}
document.write(i+" ");
//document.write(i+"</br>");
}
</script>
</body>
</html>
Output : 50 51 52 53 55 57 58 59 60
Example : A JS program to print all the values using a Label Break statement.
<html>
<body>
<script type="text/javascript">
outerloop:
for (var i = 0; i < 5; i++)
{
document.write("Outerloop value is : " + i + "<br />");
innerloop:
for (var j = 0; j < 5; j++)
{
if (j > 3 ) break ; // Quit the innermost loop
if (i == 2) break innerloop; // Do the same thing
if (i == 4) break outerloop; // Quit the outer loop
document.write("Innerloop value is : " + j + " <br />");
}
}
document.write("<br/>Program is Terminated now<br/> ");
</script>
</body>
</html>
Output :
Outerloop value is : 0
Innerloop value is : 0
Innerloop value is : 1
Innerloop value is : 2
Innerloop value is : 3
Outerloop value is : 1
Innerloop value is : 0
Innerloop value is : 1
Innerloop value is : 2
Innerloop value is : 3
Outerloop value is : 2
Outerloop value is : 3
Innerloop value is : 0
Innerloop value is : 1
Innerloop value is : 2
Innerloop value is : 3
Outerloop value is : 4
Program is Terminated now
Example : A JS program to print all the values using the Label Continue statement.
<html>
<body>
<script>
outerloop:
for (var i = 0; i < 3; i++)
{
document.write("Outerloop value is : " + i + "<br/>");
for (var j = 0; j < 5; j++)
{
if (j == 3)
{
continue outerloop;
}
document.write("Innerloop value is : " + j + "<br/>");
}
}
document.write("Program is Exiting<br /> ");
</script>
</body>
</html>
Output:
Outerloop value is : 0
Innerloop value is : 0
Innerloop value is : 1
Innerloop value is : 2
Outerloop value is : 1
Innerloop value is : 0
Innerloop value is : 1
Innerloop value is : 2
Outerloop value is : 2
Innerloop value is : 0
Innerloop value is : 1
Innerloop value is : 2
Program is Exiting
0 Comments