JavaScript Tutorial – Arithmetic Operators

JavaScript operators are used to assign values, compare values, perform arithmetic operations, and more.

JavaScript Assignment Operators

Operator = is an “assignment” operator, not an “equal to” operator. It assigns a value to its left operand based on the value of its right operand.

<script type="text/javascript">
   var website = "WhatAboutHTML";
</script>

in this example, left operand is a variable called website and the right operand is a string WhatAboutHTML.

JavaScript Arithmetic Operators

Arithmetic operators are used to perform arithmetic on numbers (literals or variables). Common arithmetic operators are addition:+, subtraction:-, multiplication:* and division:/. Please see examples below.

How to add numbers in JavaScript – Addition operator

<script type="text/javascript">
var sum = 5+1;
document.write(sum);
</script>
Try it yourself »

How to subtract numbers in JavaScript – Subtraction operator

<script type="text/javascript">
var difference = 5-1;
document.write(difference);
</script>
Try it yourself »

How to multiply numbers in JavaScript – Multiplication operator

<script type="text/javascript">
var product = 5*2;
document.write(product);
</script>
Try it yourself »

How to divide numbers in JavaScript – Division operator

<script type="text/javascript">
var num = 5/2;
document.write(num);
</script>
Try it yourself »

Addition operator can also be used to concatenate strings

You can also concatenate string and number, but the result will be a string

How to concatenate strings in JavaScript – Addition operator

<script type="text/javascript">
var text = "this is " + "JS tutorial";
var text1 = "this is " + "JS tutorial " + 2;
document.write(text);
document.write(text1);
</script>
Try it yourself »