You are currently viewing Introduction to JavaScript

Introduction to JavaScript

Share this to everyone:

JavaScript is a type of computer language that lets websites and web applications do interactive and dynamic things. It helps make web pages more than just static text and images by allowing them to respond to user actions, like clicking buttons, showing animations, and updating information on the page without needing to reload it.

Read JavaScript Fundamentals in Detail

Simple Example of JavaScript

1.Write a JavaScipt code to display the message “Hello World”.

<html>
<head>
<title> First JS program</title>
</head>
<body>
<script>
document.write("Hello World");
</script>
</body>
</html>

Output:

Hello World

2.Write a JavaScript code to input any number and display it.

<html>
<head>
<title> Second JS program</title>
</head>
<body>
<script>
var a=prompt("Enter any number");
document.write("The number is"+a);
</script>
</body>
</html>

3.Write a JavaScript Code to display the sum of two numbers.

<html>
<head>
<title> Third JS program</title>
</head>
<body>
<script>
var a=prompt("enter first number");
var b=prompt("enter second number");
var s=parseInt(a)+parseInt(b);
document.write("The sum is"+s);
</script>
</body>
</html>

4.Write a JavaScript code to calculate the area of circle.

<html>
<head>
<title> Fourth JS program</title>
</head>
<body>
<script>
var r=prompt("enter the value of R");
var a=3.14*r*r;
document.write("The area of circle is"+a);
</script>
</body>
</html>

5.Write a JavaScript code to display the simple interest.

<html>
<head>
<title> Sixth JS program</title>
</head>
<body>
<script>
var p=prompt("Enter the value of P");
var t=prompt("Enter the value of T");
var r=prompt("Enter the value of R");
var i=(p*t*r)/100;
document.write("The simple interest is"+i);
</script>
</body>
</html>

7.Write a JavaScript code to display the area of rectangle.

<html>
<head>
<title>Seventh Js</title>
</head>
<body>
<script>
var l=prompt("Enter the value of l");
var b=prompt("Enter the value of b");
var a=l*b;
document.write("The area of rectangle is "+a);
</script>
</body>
</html>

Question:

  1. Write a JS code to calculate the volume
    of cuboid. [v=l*b*h]
  2. Write a JS code to calculate the perimeter of
    circle. [p=2*3.14*r]
  3. Write a JS code to display the perimeter of
    rectangle. [p=2*(l+b)]

8. Program to input any number and check odd or even.

<html>
<head>
<title>ODD or Even</title>
</head>
<body>
<script>
var a=prompt("Enter any number");
if(a%2==0)
document.write("It is even");
else
document.write("It is odd");
</script>
</body>
</html>

9. Program to check whether a number is divisible by 5 or not.

<html>
<head>
<title>Divisible by 5 or not</title>
</head>
<body>
<script>
var a=prompt("Enter any number");
if(a%5==0)
document.write("It is divisible by 5");
else
document.write("It is not divisible by 5");
</script>
</body>
</html>

10. program to check whether a number is divisible by 5 and 11 or not.

<html>
<head>
<title>Divisible by 5 and 11</title>
</head>
<body>
<script>
var a=prompt("Enter any number");
if(a%5==0 && a%11==0)
document.write("It is divisible by 5 and 11");
else
document.write("It is not divisible by 5 and 11");
</script>
</body>
</html>

11. program to check whether a number is divisible by 5 or 11 or not.

<html>
<head>
<title>Divisible by 5 or not</title>
</head>
<body>
<script>
var a=prompt("Enter any number");
if(a%5==0 || a%11==0)
document.write("It is divisible by 5 or 11");
else
document.write("It is not divisible by 5 and 11");
</script>
</body>
</html>

12. program to input any number and display its factors.

<html>
<head>
<title>Factors of a number</title>
</head>
<body>
<script> 
var a=prompt("Enter any number");
bar b;
for(b=1;b<=a;b++)
{
if(a%b==0)
document.write(b+"<br>");
}
</script>
</body>
</html>

13. Program to display the factorial of a number.

<html>
<head>
<title>Factorial of a number</title>
</head>
<body>
<script>
var a=prompt("Enter any number");
var b,f=1;
for(b=1;b<=a;b++)
{
f=f*b;
}
document.write("The factorial is "+f);
</script>
</body>
</html>

14. Program to display the multiplication table of a number.

<html>
<head>
<title> Table of a number</title>
</head>
<body>
<script>
var a=prompt("Enter any number");
var b;
for(b=1;b<=10;b++)
{
document.write(a+"*"+b+"="+a*b+"<br>");
}
</script>
</body>
</html>

14. Program to check whether a number is prime or composite.

<html>
<head>
<title>Prime or composite</title>
</head>
<body>
<script>
var a=prompt("Enter any number");
var b,c=0;
for(b=1;b<=a;b++)
{
if(a%b==0)
c=c+1;
}
if(c==2)
document.write("The  number is prime");
else
document.write("The number is composite");
</script>
</body>
</html>

15. Program to input any number and display the sum of each digit.

<html>
<head>
<title>Sum of Each digit</title>
</head>
<body>
<script>
var a=prompt("Enter any number");
var r,s=0;
while(a!=0)
{
r=a%10;
s=s+r;
a=parseInt(a/10);
}
document.write("The sum is"+s);
</script>
</body>
</html>

16. Program to input any number and display the sum of even digits only.

<html>
<head>
<title>Sum of even digit</title>
</head>
<body>
<script>
var a=prompt("Enter any number");
var r,s=0;
while(a!=0)
{
r=a%10;
if(r%2==0)
s=s+r;
a=parseInt(a/10);
}
document.write("The sum is"+s);
</script>
</body>
</html>

17. Program to input any number and display the sum of odd digits only.

<html>
<head>
<title>Sum of odd digit</title>
</head>
<body>
<script>
var a=prompt("Enter any number");
var r,s=0;
while(a!=0)
{
r=a%10;
if(r%2==1)
s=s+r;
a=parseInt(a/10);
}
document.write("The sum of odd digit is"+s);
</script>
</body>
</html>

18. Program to input any number and display the product of each digit.

<html>
<head>
<title>Sum of Each digit</title>
</head>
<body>
<script>
var a=prompt("Enter any number");
var r,p=1;
while(a!=0)
{
r=a%10;
p=p*r;
a=parseInt(a/10);
}
document.write("The product is"+p);
</script>
</body>
</html>

Do it yourself:

19. Program to input any number and display the product of even digit only.

20. Program to input any number and display the product of odd digits only.

21. program to display the numbers from 1 to 10. OR program to display: 1 2 3…..upto 10th term

<html>
<head>
<title>Numbers from 1 to 10</title>
</head>
<body>
<script>
var a;
for(a=1;a<=10;a++)
{
document.write(a+"&emsp;");
}
</script>
</body>
</html>

22. Program to display the even numbers from 1 to 20. or program to display: 2 4 6 upto 10 th term.

<html>
<head>
<title>Even Numbers</title>
</head>
<body>
<script>
var a;
for(a=2;a<=20;a=a+2)
{
document.write(a+"&emsp;");
}
</script>
</body>
</html>

23. program to display the odd numbers from 1 to 20. or program to display: 1 3 5 upto 10 th term.

<html>
<head>
<title>Odd Numbers</title>
</head>
<body>
<script>
var a;
for(a=1;a<=20;a=a+2)
{
document.write(a+"&emsp;");
}
</script>
</body>
</html>

24. Program to display: 5 10 15 upto nth term.

<html>
<head>
<title> Series </title>
</head>
<body>
<script>
var n=prompt("Enter the limit of series");
var a,b=5;
for(a=1;a<=n;a++)
{
document.write(b+"&emsp;");
b=b+5;
}
</script>
</body>
</html>

25. program to display the Fibonacci series:
1 1 2 3 5 upto 10th term
.

<html>
<head>
<title> Important Series </title>
</head>
<body>
<script>
var a=0,b=1,c=0,d;
for(d=1;d<=10;d++)
{
a=b+c;
document.write(a+"&emsp;");
b=c;
c=b;
}
</script>
</body>
</html>

26. program to display: 5 55 555 5555 up to nth term.

<html>
<head>
<title> Repeated number Series </title>
</head>
<body>
<script>
var n=prompt("Enter the limit of your series");
var a,b=5;
for(a=1;a<=n;a++)
{
document.write(b+"&emsp;");
b=b*10+5;
}
</script>
</body>
</html>

27. program to display:
1 4 9 16 upto 10th term.



<html>
<head>
<title> Square Series </title>
</head>
<body>
<script>
var a;
for(a=1;a<=10;a++)
{
document.write(a*a+"&emsp;");
}
</script>
</body>
</html>

This Post Has One Comment

Comments are closed.