Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
JavaScript Program for subtracting Two Numbers | 4 different ways

JavaScript Program for subtracting Two Numbers | 4 different ways

Posted on October 23, 2022

Table of Contents

  • JavaScript Program for subtracting Two Numbers | 4 different ways
    • Code to find difference  of two numbers in JS
      • Subraction of two numbers in JS
      • Subtraction of two numbers in JS using HTML
      • Subtraction of two numbers in JS using function – #1
      • Subtraction of two numbers in JS using function – #2

JavaScript Program for subtracting Two Numbers | 4 different ways

In this article, we will discuss the concept of JavaScript Program for Subtracting Two Numbers

In this post, we are going to learn how to write a program to find  subtraction of two numbers and dispay result on the screen in JS language

Code to find difference  of two numbers in JS

Subraction of two numbers in JS

In this program, the user initiates the values to variables f_Num and l_Num then calculates the difference of the given numbers using minus(-) operator

Program 1

const f_Num=17;
//declare and initaiize variable for first num
const l_Num=13;
//declare and initaiize variable for second num

//calculate difference of given two numbers
const diff=f_Num-l_Num;

//display the difference of two numbers
console.log('The difference of '+f_Num+' and '+l_Num+' is: '+diff);

When the above code is executed, it produces the following result

The difference of 17 and 13 is: 4

Subtraction of two numbers in JS using HTML

In this program, the user initiates the values to variables firstNum and lastNum then calculates the difference of the given numbers using minus(-) operator inside the HTML tag

Program 1

<html>
<head>
<title>Subtraction of two numbers</title>
<script>
var firstNum=32;
var lastNum=20;//Decare and initiaize variabes
var sub=firstNum-lastNum;//Calculate difference of given numbers 
document.write("subtraction of "+firstNum+" and "+lastNum+" is: "+sub);
//print result on the screen
</script>
</head>
<body>

</body>
</html>

When the above code is executed, it produces the following result

subtraction of 32 and 20 is: 12

 

Subtraction of two numbers in JS using function – #1

In this program, the program is asked to enter two numbers from the user and calculates the difference of the given numbers using the function in JavaScript language

Program 3

<html>
<head>
<title>subtract two numbers</title>
<script>
function subs()//function to subtraction
{
var f_Num,s_Num,dif;
f_Num=Number(document.getElementById("first").value);
s_Num=Number(document.getElementById("second").value);
dif=f_Num-s_Num;
document.getElementById("ans").innerHTML="Difference:"+dif;
}
</script>
</head>
<body>
<p>Enter the first Number:<input type="text" id="first"></p>
<p>Enter the second Number:<input type="text" id="second"></p>
<button onclick="subs();">Subtract</button>
<p id="ans"></p>
</body>
</html>

When the above code is executed, it produces the following result

JavaScript Program for subtracting Two Numbers | 4 different ways
Output 3

 

Subtraction of two numbers in JS using function – #2

In this program, the program is asked to enter two numbers from the user and calculates the difference of the given numbers using the function in JavaScript language

Program 4

<html>
<head>
<title>Difference of two numbers</title>
<script>
function sub()//function for subtraction
{
var f_Num,s_Num,dif;
f_Num=Number(document.getElementById("first").value);
s_Num=Number(document.getElementById("second").value);
dif=f_Num-s_Num;
document.getElementById("ans").innerHTML="Difference:"+dif;
}
function clr()//function to clear all fields
{
document.getElementById("first").value="";
document.getElementById("second").value="";
document.getElementById("ans").innerHTML="";
}
</script>
</head>
<body>
<p>Enter the first value:<input type="text" id="first"></p>
<p>Enter the second value:<input type="text" id="second"></p>
<button onclick="sub();">Subtract</button>
<button onclick="clr();">clear</button>
<p id="ans"></p>
</body>
</html>

When the above code is executed, it produces the following result

After subtraction

JavaScript Program for subtracting Two Numbers | 4 different ways
Output 4

 

After clear

JavaScript Program for subtracting Two Numbers | 4 different ways
Output 5

 

Suggested post

Subtract two number in Java language

Subtract two number in C language

Subtract two number in C++ language

Subtract two number in Python language

 

Sum of two integers in C#

Sum of two integers using function in C#

Sum of two floating point numbers in C#

 

Addition two number  in Java language

Addition two number in C language

Addition two number  in C++ language

Addition two number in Python language

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes