Arithmetic Operators

Operator Types

Unary

A unary operator requires a single operand, either before or after the operator, following this format:

operand operator
operator operand

For example, in the expression a++, ++ is a unary operator.

Binary

A binary operator requires two operands, one before the operator and one after the operator, following this format:

operand1 operator operand2

For example, in the expression a + b = c, + is a binary operator.

Ternary

There is one ternary operator, the conditional operator. For example, in the expression a ? b : c, the use of ? and : in this manner constitutes the ternary operator. We'll discuss this operator more in the Conditional Statements tutorial.

Arithmetic Operators

An arithmetic operator takes numeric values (either literals or variables) as its operands and returns a single numeric value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). Other arithmetic operators are remainder (%), unary negation (-), unary plus (+), increment (++), decrement (--), and exponentiation (**).

1. Addition (+)

We use this operator in the form operand1 + operand_2. For example:

2 + 3 // evaluates to 5
4 + 10 // evaluates to 14

2. Subtraction (-)

We use this operator in the form operand1 - operand2. For example:

3 - 2 // evaluates to 1
4 - 10 // evaluates to -6

3. Multiplication (*)

We use this operator in the form operand1 * operand2. For example:

3 * 2 // evaluates to 6
4 * 10 // evaluates to 40

4. Division (/)

We use this operator in the form operand1 / operand2. For example:

6 / 3 // evaluates to 2
3 / 2 // evaluates to 1.5
4 / 10 // evaluates to 0.4

5. Remainder (%)

We use this operator in the form operand1 % operand2. For example:

6 % 3 // evaluates to 0
3 % 2 // evaluates to 1
4 % 10 // evaluates to 4

6. Exponentiation (**)

We use this operator in the form operand1 ** operand2. This operator is a part of ECMAScript2016 feature set. For example:

2 ** 3 // evaluates to 8
3 ** 2 // evaluates to 9
5 ** 4 // evaluates to 625

7. Unary Negation (-)

We use this operator in the form -operand. For example:

-4 // evaluates to -4
-(-5) // evaluates to 5 (not --5)

8. Unary Plus (+)

We use this operator in the form +operand. For example:

+4 // evaluates to 4
+(-4) // evaluates to -4

9. Increment (++)

We use this operator in the prefix and postfix forms, forms ++operand and operand++. The prefix form, ++operand, increments the operand by and then returns the value of the operand. The postfix form, operand++, returns the value of the operand and then increments the operand's value by . For example:

EXAMPLE
Run
Input
Output

Solution

The code above produces this output:

a(4), ++a(5)
a(6), b(5), b++(5)
a(6), b(6)

10. Decrement (--)

We use this operator in the prefix and postfix forms, forms --operand and operand--. The prefix form, --operand, decrements the operand by and then returns the value of the operand. The postfix form, operand--, returns the value of the operand and then decrements the operand's value by . For example:

EXAMPLE
Run
Input
Output

Solution

The code above produces this output:

a(4), --a(3)
a(2), b(3), b--(3)
a(2), b(2)

If-Else Conditional StatementsRecommended Article
View

If-Else Conditional Statements

Switch Conditional StatementsRecommended Article
View

Switch Conditional Statements

 
JavaScript: Arithmetic Operators
Go to Top
  1. Challenge Walkthrough
    Let's walk through this sample challenge and explore the features of the code editor.1 of 6
  2. Review the problem statement
    Each challenge has a problem statement that includes sample inputs and outputs. Some challenges include additional information to help you out.2 of 6
  3. Choose a language
    Select the language you wish to use to solve this challenge.3 of 6
  4. Enter your code
    Code your solution in our custom editor or code in your own environment and upload your solution as a file.4 of 6
  5. Test your code
    You can compile your code and test it for errors and accuracy before submitting.5 of 6
  6. Submit to see results
    When you're ready, submit your solution! Remember, you can go back and refine your code anytime.6 of 6
  1. Check your score