w3resource logo


JavaScript Operators

JavaScript : Arithmetic Operators

Secondary Nav

Arithmetic Operators : +, -, *, /

In JavaScript, arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. There are four standard  arithmetic operators, addition (+), subtraction (-), multiplication (*), and division (/).

These operators work as they do in other programming languages except the division (/) operator which returns a floating-point division in JavaScript, not a truncated division as it does in languages such as C or Java.
 
For example:
1/2  returns 0.5 in JavaScript.
1/2  returns 0 in Java.

In addition, JavaScript provides modules(%), increment(++), decrement(--) and unary negation(-) operators.

Example : ( + addition operator)

HTML Code:

<!doctype html>
<head>
<meta charset="utf-8">
<title>JavaScript + operator example</title>
<meta name="description" content="This document contains
an example of JavaScript + operator" />
</head>
<body>
<script src="addition_example1.js"></script>
</body>
</html>

JS Code:

var var1 = 45;
var var2 = 78;
var var3 = 45.10;
var var4 = 178.12;
var newvar  = var1 + var2;
var newvar1 = var3 + var4;
var newParagraph3 = document.createElement("p");
var newText3 = document.createTextNode("var1 + var2 = "+newvar+" and var3 + var4 = "+newvar1);
newParagraph3.appendChild(newText3);
document.body.appendChild(newParagraph3);

View the example in the browser

Practice the example online

JavaScript + operator example

Example : ( - subtraction operator)

HTML Code:

<!doctype html>
<head>
<meta charset="utf-8">
<title>JavaScript subtraction operator example </title>
<meta name="description" content="This document contains an
example Using JavaScript subtraction operator"/>
</head>
<script src="subtraction-example1.js"></script>
</body>
</html>

JS Code:

var var1 = 45;
var var2 = 78;
var str1 = "w3resource";
var str2 = ".com";
var newvar = var1 - var2;
var newstr = str1 - str2;
var varstr = var1 - str2;
var newParagraph = document.createElement("p");
var newText = document.createTextNode("var1 -var2 = "+ newvar);
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode("str1 - str2  = "+ newstr);
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
var newParagraph2 = document.createElement("p");
var newText2 = document.createTextNode("var1 - str2  = "+ varstr);
newParagraph2.appendChild(newText2);
document.body.appendChild(newParagraph2)

View the example in the browser

Practice the example above online

JavaScript subtraction operator example

Example : ( * multiplication operator)

HTML Code:

<!doctype html><head>
<meta charset="utf-8">
<title>JavaScript multiplication operator (*) example with DOM
</title>
<meta name="description" content="This document contains an example
Using JavaScript multiplication operator (*) with dom"/>
</head>
<body>
<script src="multiplication-example1.js"></script>
</body>
</html>

JS Code:

var var1 = 45;
var var2 = 78;
var var3 = 45.10;
var var4 = 178.12;
var newvar  = var1 * var2;
var newvar1 = var3 * var4;

var newParagraph = document.createElement("p");
var newText = document.createTextNode("var1 * var2 = "+ newvar);
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);

var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode("var3 * var4  = "+ newvar1);
newParagraph1.appendChild newText1);
document.body.appendChild(newParagraph1);

View the example in the browser

Practice the example online

JS Bin

Example : ( / division operator)

HTML Code:

<!doctype html>
<head>
<meta charset="utf-8">
<title>JavaScript / operator example </title>
<meta name="description" content="This document contains an example of JavaScript division operator" />
</style>
</head>
<body>
<script src="division-example1.js"></script>
</body>
</html>

JS Code:

var var1 = 45;
var var2 = 78;
var var3 = 1;
var var4 = 2;
var newvar  = var1 / var2;
var newvar1 = var3 / var4;

var newParagraph = document.createElement("p");
var newText = document.createTextNode ("var1 / var2 = "+ newvar);
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);

var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode("var3 / var4  = "+ newvar1);
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);

View the example in the browser

Practice the example online

JavaScript / operator example



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program