Arithmetic Operators - Addition, Subtraction, Multiplication and Division

In JavaScript, we have arithmetic operators that allow us to perform mathematical operations on numbers.

The + operator is used to add numbers together, for example:

Loading Code . . .

The - operator is used to subtract numbers, for example:

Loading Code . . .

The * operator is used to multiply numbers, for example:

Loading Code . . .

The / operator is used to divide numbers, for example:

Loading Code . . .

As in math, the order of operations applies to arithmetic operators in JavaScript. For example, the following expression will be evaluated as 2 + 3 * 4:

Loading Code . . .

Because multiplication has a higher precedence than addition, the expression will be evaluated as 2 + 12, which is 14.

To change the order of operations, we can use parentheses. For example, the following expression will be evaluated as (2 + 3) * 4:

Loading Code . . .

Here, the parentheses ensure that the addition operation is performed before the multiplication.

  • + (plus) for addition
  • - (minus) for subtraction
  • * (asterisk) for multiplication
  • / (slash) for division

In the code below, we have a variable called x that is assigned the value of 5 and a variable called y that is assigned the value of 2.

Select the correct operators for each line of code to make the output of the console.log statements correct.

Loading Code . . .

console.log(x ? y); // output: 10

console.log(x ? y); // output: 7

console.log(x ? y); // output: 2.5

console.log(x ? y); // output: 3

© 2024 - ®Mewters