JavaScript – Exercises, Practice, Solution | JavaScript programming Examples

1. Program to print Hello World
<html>
<head>
<script language=”JavaScript”>
document.write(“<font color=’red’ size=’4′>Hello world</font>”)
</script>
</head>
</html>

Output:

2. Addition of two numbers

<html>

<head>

<title>Adding two numbers </title>

 <script>

         var num1 = 10;

         var num2 = 40;

         var sum = num1+num2;

document.write(“Sum: ” + sum);

</script>

</head>

</html>

Output:

Sum: 50

3. Program to Add, subtract, multiply, and divide

<html>

<head>

<script>

var num1=100,num2=10,result;

result=num1+num2;

document.write(“Add = ” + result + “<br>”);

result=num1-num2;

document.write(“Subtract = ” + result + “<br>”);

result=num1*num2;

document.write(“Multiply = ” + result + “<br>”);

result= num1/num2;

document.write(“Divide = ” + result + “<br>”);

</script>

</head>

</html>

Output:

Add = 110
Subtract = 90
Multiply = 1000
Divide = 10

4.  Program to input value from the user using prompt

<html>

 <head>

 <script>

var name=prompt(“Enter your name: “);

document.write(“Welcome “+ name);

</script>

<style>

body {

        color: #FFFF00;

        background: #EE82EE;

        font-size: 30px;

     }

</style>

</head>

<body>

</body>

 </html>

Output:

5. Input an integer value and print it

<html>

<head>

<script>

var num = prompt(“Enter an integer value:”);

document.write(“Entered Number is “, num);

</script>

</head> 

</html>

Output:

6. Add two numbers taking input from user

<html>

<head>

<title>Add two numbers</title>

<script language=”JavaScript”>

var num1=parseInt(prompt(“Enter first number”));

var num2=parseInt(prompt(“Enter second number”));

var sum=num1+num2

alert(“Sum of two numbers : ” + sum)

</script>

</head>

</html>

Output:

7. Program to add two float values 

<html>

<head>

<script>

var num1 = 4.12;

var num2 = 9.87;

var result = 0;

// Adding float values

result =num1+ num2;

// Without rounding the result

document.write(“Without rounding: ” + result)

// Rounding the result to 2 digits after decimal

result = result.toFixed(2);

document.write(“<br>With rounding: ” + result)

</script>

</head>

</html>

Output:

Without rounding: 13.989999999999998

With rounding: 13.99

8. Program to swap values of two variables

<html>

<head>

<title>Swapping values of two variables</title>

<script>

//take input from the users

let a = prompt(‘Enter the value of a: ‘);

let b = prompt(‘Enter the value of b: ‘);

document.write(“The value of a and b before swapping: ” + a + ” ” + b)

//create a temporary variable

let temp;

//swap variables

temp = a;

a = b;

b = temp;

document.write(“<br>The value of a and b after swapping: ” + a + ” ” + b)

</script>

</head>

</html>

Output:

The value of a and b before swapping: 10 20
The value of a and b after swapping: 20 10

9. Program to check if the number is even or odd

<html>

<head>

<script>

// take input from the user

var num= prompt(“Enter a number: “);

//check if the number is even

if(num%2==0)

document.write(num + ” is an Even Number”);

else

document.write(num + ” is an Odd Number”);

</script>

</head>

</html>

Output:

12 is an Even Number

10. Program to check leap year

<html>

<head>

<script>

// program to check leap year

function checkLeapYear(year) {

if(year%4==0) {

document.write(year + ‘ is a leap year’);

}

else {

document.write(year + ‘ is not a leap year’);

    }

}

// take input

const year=prompt(‘Enter a year:’);

checkLeapYear(year);

</script>

</head>

</html>

Output:

2020 is a leap year

11. Program to find the largest among three numbers

<html>

<head>

<script>

// take input from the user

const num1 = prompt(“Enter first number: “);

const num2 = prompt(“Enter second number: “);

const num3 = prompt(“Enter third number: “);

let largest;

// check the condition

if(num1>num2&& num1>num3) {

    largest= num1;

}

else if (num2>num1&&num2>num3) {

    largest = num2;

}

else {

    largest = num3;

}

// display the result

document.write(“The largest number is: ” + largest);

</script>

</head>

<html>

Output:

The largest number is: 23

12. Program that checks if the number is positive, negative or zero

<html>

<head>

<script>

// input from the user

const number = prompt(“Enter a number: “);

// check if number is greater than 0

if (number > 0) {

document.write(“The number is positive”);

}

// check if number is 0

else if (number == 0) {

document.write(“The number is zero”);

}

// if number is less than 0

else {

document.write(“The number is negative”);

}

</script>

</head>

</html>

Output:

The number is positive

The number is zero

The number is negative

13. Program to input age and check person is eligible for voting or not

<html>

<head>

<script>
 function validate(age) {
      if(age>=18) {
             ans=”eligible”;
            }
      else {
             ans=”not eigible”;
              }
document.write(“You are <b>”+ ans +”</b> for Vote”);
        }
var age=prompt(“Enter age”);
validate(age);
</script>
</html>
14. Program to Add n numbers 

<html>

<body>

<script>

  var i, n=5, sum=0;

  arr = new Array(20, 22, 13, 15, 19);

  for(i=0; i<n; i++)

sum = sum + arr[i];

document.write(“Sum of 5 numbers: ” + sum);

</script>

</body>

</html>

Output:

Sum of 5 numbers: 89

16. Program to Demonstrate Popup Boxes in JavaScript

<html>

<head>

<title>Popup Boxes in JavaScript</title>

<script>

alert(“Welcome to Our website!”);

confirm(“It is a Confirm popup box!”);

var name=prompt(“This is a prompt box!”, “Enter your name: “);

alert(“Welcome “+ name);

</script>

</head>

<body bgcolor=”seagreen”>

<div style=”text-align:center; color: #FFFF00; background: #EE82EE; font-size: 30px;”>

<h1>JavaScript Example to Demonstrate Popup Boxes!</h1>

</div>

</body>

</html>

Output:

Leave a Comment