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.
var website = "WhatAboutHTML";
In the above example, the 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: /
. See examples below.
How to add numbers in JavaScript – Addition operator
var sum = 5+1;
document.write("5 + 1 = " + sum);
How to subtract numbers in JavaScript – Subtraction operator
var difference = 5-1;
document.write("5 - 1 = " + difference);
How to multiply numbers in JavaScript – Multiplication operator
var product = 5*2;
document.write("5 * 2 = " + product);
How to divide numbers in JavaScript – Division operator
var num = 5/2;
document.write("5 / 2 = " + num);
How to Concatenate Strings in JavaScript
The 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 combine strings in JavaScript
var text = "This is " + "JS tutorial
";
var text1 = "This is " + "JavaScript tutorial " + 2;
document.write(text);
document.write(text1);