JavaScript
Introduction:
JavaScript is standard client-side scripting programming language for developing dynamic
web based application.
Features of JavaScript:
It is a standard scripting language and supported by most of the browsers.
It is a lightweight and efficient interpreted programming language.
It is complementary to and integrated with HTML and java.
It is object-oriented event driven programming language.
It is open source and cross platform (independent to OS).
Importance of JavaScript:
It is designed for solving client-side application with prompt responds.
It can dynamically modify webpage as per requirement.
It can validate and respond user's input.
It can be used to create cookies. (A piece of information stored on the user's computer
by the web browser while browsing a website)
It does not require any interaction to web server while processing scripts on web
browser.
A JavaScript program to calculate the simple interest.
60
Source code:
<!DOCTYPE html>
<html>
<head><title>Simple Interest</title></head>
<body>
<script type="text/javascript">
var p=prompt("Enter principal amount:");
var r=prompt("Enter rate of interest:");
var t=prompt("Enter time in Years:");
var s;
s=p*t*r/100;
[Link]("Simple interest is Rs."+s);
</script>
</body>
</html>
Output:
61
A JavaScript program to check the entered number is even or
odd.
Source code:
<!DOCTYPE html>
<html>
<head><title>Even or Odd</title></head>
<body>
<script type="text/javascript">
var n=prompt("Enter a number:");
if(n%2==0)
{
[Link](n+" is even number.");
}
else
{
[Link](n+" is odd number.");
}
</script>
</body>
</html>
Output:
62
A JavaScript program to find the factorial of the entered number.
Source code:
<!DOCTYPE html>
<html>
<head><title>Factorial</title></head>
<body>
<script type="text/javascript">
function fact(n)
{
var i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
var n=prompt("Enter a number:");
if(n<0)
{
[Link]("Factorial doesn't exit!");
}
else
{
[Link]("<br>Factorial of "+n+" is "+fact(n));
}
</script>
</body>
</html>
63
Output:
64
A JavaScript program to find largest number among 3 numbers.
Source code:
<!DOCTYPE html>
<html>
<head><title>FLargest among 3 numbers</title></head>
<body>
<script type="text/javascript">
var a=parseInt(prompt("Enter first number:"));
var b=parseInt(prompt("Enter second number:"));
var c=parseInt(prompt("Enter third number:"));
if(a>b&&a>c)
{
[Link]("The largest number is "+a);
}
else if(b>c)
{
[Link]("The largest number is "+b);
}
else
{
[Link]("The largest number is "+c);
}
</script>
</body>
</html>
Output:
65
A JavaScript program to display even numbers up to 20.
Source code:
<!DOCTYPE html>
<html>
<head><title>Even number series</title></head>
<body>
<script type="text/javascript">
var i;
[Link]("Even numbers upto 20:<br>");
for(i=1;i<=20;i++)
{
if(i%2==0)
{
[Link](" "+i);
}
}
</script>
</body>
</html>
Output:
66
A JavaScript program to check whether the number is prime or
composite.
Source code:
<!DOCTYPE html>
<html>
<head><title>Prime or composite</title></head>
<body>
<script type="text/javascript">
function checkprime(n)
{
var i;
for(i=2;i<=n;i++)
{
if(n%i==0)
break;
}
if(n==i)
{
return 1;
}
else
{
return 0;
}
}
var n=parseInt(prompt("Enter a number:"));
if(checkprime(n)==1)
{
[Link](n+" is prime number.");
}
else
{
[Link](n+" is composite number.");
67
}
</script>
</body>
</html>
Output:
68
A JavaScript program to calculate the area of rectangle by using
function.
Source code:
<!DOCTYPE html>
<html>
<head><title>Area of rectangle</title></head>
<body>
<script type="text/javascript">
function area(l,b)
{
return l*b;
}
var l=parseInt(prompt("Enter length of rectangle:"));
var b=parseInt(prompt("Enter breadth of rectangle:"));
var a=area(l,b);
[Link]("Area of rectangle is "+a);
</script>
</body>
</html>
Output:
69
A JavaScript program to find the reverse of the entered number.
Source code:
<!DOCTYPE html>
<html>
<head><title>Reverse of number</title></head>
<body>
<script type="text/javascript">
var r,rev=0,temp;
var n=parseInt(prompt("Enter a number:"));
temp=n;
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=[Link](n/10);
}
[Link](temp+" Reverse number is "+rev);
</script>
</body>
</html>
Output:
70
A JavaScript program to check the entered number is palindrome
or not.
Source code:
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var r,sum=0,temp;
var n=parseInt(prompt("Enter a number:"));
while(n!=0)
{
r=n%10;
sum=sum*10+r;
n=[Link](n/10);
}
if(temp==sum)
{
[Link](temp+" is a palindrome number.");
}
else
{
[Link](temp+" is not a palindrome number.");
}
</script>
</body>
</html>
Output:
71
A JavaScript program to check if the entered number is
Armstrong or not.
Source code:
<!DOCTYPE html>
<html>
<head><title>Armstrong or not</title></head>
<body>
<script type="text/javascript">
var r,i=0,sum=0,temp;
var num=parseInt(prompt("Enter a number:"));
temp=num;
for(var n=num;n>0;n=[Link](n/10))
{
i++;
}
while(num!=0)
{
r=num%10;
sum=sum+[Link](r,i);
num=[Link](num/10);
}
if(temp==sum)
{
[Link](temp+" is an armstrong number.");
}
else
{
[Link](temp+" is not an armstrong number.");
}
</script>
</body>
</html>
72
Output:
73
A JavaScript program to print largest and smallest numbers
among 10 elements of an array.
Source code:
<!DOCTYPE html>
<html>
<head><title>Smallest and largest</title></head>
<body>
<script type="text/javascript">
var a= new Array(10);
var i,j,temp;
for(i=0;i<10;i++)
{
a[i]=parseInt(prompt("Enter a number:"));
}
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
[Link]("Largest number is "+a[9]);
[Link]("<br>Smallest number is "+a[0]);
</script>
</body>
</html>
74
Output:
75
A JavaScript program to display the multiplication table of the
entered number.
Source code:
<!DOCTYPE html>
<html>
<head><title>Multiplication table</title></head>
<body>
<script type="text/javascript">
var n=parseInt(prompt("Enter a number:"));
var i;
[Link]("Multiplication table of "+n);
for(i=0;i<=10;i++)
{
[Link]("<br>"+n+"*"+i+" = "+(n*i));
}
</script>
</body>
</html>
Output:
76
A JavaScript program to add two numbers on click event
handling concept.
Source code:
<!DOCTYPE html>
<html>
<head><title>Event handling</title></head>
<body>
<script type="text/javascript">
function sum(a,b)
{
var s=a+b;
[Link]("Sum is "+s);
}
var a=parseInt(prompt("Enter a number:"));
var b=parseInt(prompt("Enter another number:"));
[Link]("Click for sum:");
</script>
<input type="button" ></body>
</html>
Output:
After clicking on click button:
77
A JavaScript program to create an object.
Source code:
<!DOCTYPE html>
<html>
<head><title>Object</title></head>
<body>
<script type="text/javascript">
var phone={
make:"Apple",
series:"i phone 16 pro max",
year:"2024"
};
[Link]([Link]+"--"+[Link]+"--"+[Link]);
</script>
</body>
</html>
Output:
78
A JavaScript program to validate form.
Source code:
<!DOCTYPE html>
<html>
<head><title>Form validation</title>
<script type="text/javascript">
function validate()
{
if([Link]=="")
{
alert("Please provide your name!");
[Link]();
return false;
}
if([Link]=="")
{
alert("Please provide your EMail!");
[Link]();
validateEMail()
return false;
}
if([Link]==""||isNaN([Link])||
[Link]!=5)
{
alert("Enter zip in the format #####");
[Link]();
return false;
}
if([Link]=="-1")
{
alert("Please provide your country!");
return false;
}
79
return(true);
}
</script>
</head>
<body>
<form action="[Link]" name="myForm" > <table cellspacing="2" cellpadding="2" border="0">
<tr>
<td align="right">Name</td>
<td><input type="text" name="Name"></td>
</tr>
<tr>
<td align="right">EMail</td>
<td><input type="text" name="EMail"></td>
</tr>
<tr>
<td align="right">Zip Code</td>
<td><input type="text" name="Zip"></td>
</tr>
<tr>
<td align="right">Country</td>
<td><select name="Country">
<option value="-1" selected>Choose Country</option>
<option value="1">USA</option>
<option value="2">UK</option>
<option value="3">Nepal</option>
</select></td>
</tr>
<tr>
<td align="right"></td>
<td><input type="submit" value="Submit"></td>
</tr>
80
</table>
</form>
</body>
</html>
Output:
CONCLUSION :
81
This project, consisting of 15 beginner-friendly JavaScript programs, has effectively
introduced core concepts such as data types, functions, event handling, and the jQuery
library. Each program provided practical examples to reinforce foundational skills, making it
easier for beginners to understand and apply JavaScript in real-world scenarios. By exploring
these programs, learners have gained a solid understanding of how to create dynamic and
interactive web applications. This collection serves as a valuable resource for building
confidence and preparing for more advanced JavaScript development.
82