JavaScript Arithmetic Operators
In this article we look at arithmetic operators in JavaScript. Many of the operators are the same as in basic math, so this may seem like a review of elementary school, but some of them may be new to you and it's important to know that you can use them in JavaScript. In the examples below, we mostly use numbers, but in real programs you usually use variables.
All of these except for modulus should be familiar to you from elementary school. Modulus (%) gives the remainder from doing integer division. So 13 % 4 means that you divide 13 by 4, this results in 3 with a remainder of 1. Thus 13 % 4 is 1.
You can also use the addition operator with strings. In this case it concatenates (joins) the two strings together. A common mistake is adding (or forgetting) a space between the two strings you are joining.
Often you want to add (or subtract, multiply or divide) a variable by some value and then assign the result to the original variable. The assignment operator can be combined with these operators to do this in one step.
Happy Scripting!
Operator | Example | Result | |
---|---|---|---|
assignment | a is given the value 4 | ||
multiplication | |||
division | |||
modulus | |||
addition | |||
subtraction |
All of these except for modulus should be familiar to you from elementary school. Modulus (%) gives the remainder from doing integer division. So 13 % 4 means that you divide 13 by 4, this results in 3 with a remainder of 1. Thus 13 % 4 is 1.
You can also use the addition operator with strings. In this case it concatenates (joins) the two strings together. A common mistake is adding (or forgetting) a space between the two strings you are joining.
Operator | Example | ||
---|---|---|---|
addition | |||
Often you want to add (or subtract, multiply or divide) a variable by some value and then assign the result to the original variable. The assignment operator can be combined with these operators to do this in one step.
Expression | Shortcut |
---|---|
Happy Scripting!
This site needs an editor - click to learn more!
You Should Also Read:
JavaScript Resources
Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map
Content copyright © 2023 by Julie L Baumler. All rights reserved.
This content was written by Julie L Baumler. If you wish to use this content in any manner, you need written permission. Contact
BellaOnline Administration
for details.