SimpleTuts.com

Arithmetic Operators in JavaScript

JavaScript arithmetic operators perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%), which computes the remainder of a division operation. They are essential for performing calculations and manipulating numeric data in JavaScript programs.

All in one Example:


<html>
<head>
	<title>Arithmatic Example</title>
</head>
<body>
	<label>Enter first number</label>
	<input type="number" id="num1">
	<br><br>
	<label>Enter second number</label>
	<input type="number" id="num2">
	<br><Br>
	<button onclick="calculate();">submit</button>
<script>
	function calculate(){
		var n1,n2;
		n1=Number(document.getElementById("num1").value);
		n2=Number(document.getElementById("num2").value);
		
		var sum = n1+n2; 
		var dif = n1-n2;
		var pro = n1*n2;
		var quo = n1/n2;
		var rem = n1%n2;
		var exp = n1**n2;
		
		document.write("<h1>First number = "+n1+"</h1>");
		document.write("<h1>Second number = "+n2+"</h1>");
		document.write("<hr>");
		document.write("<h1>Sum = "+sum+"</h1>");
		document.write("<h1>Difference = "+dif+"</h1>");
		document.write("<h1>Product = "+pro+"</h1>");
		document.write("<h1>Quotient = "+quo+"</h1>");
		document.write("<h1>Reminder = "+rem+"</h1>");
		document.write("<h1>Exponentiation = "+exp+"</h1>");
	}
</script>	
</body>
</html>
Run Code

Output(before):



Output(after):