Javascript Arithmetic Operators

In JavaScript, arithmetic operators are used to perform arithmetic operations on numerical values. The basic arithmetic operators include addition (+), subtraction (-), multiplication (*), and division (/).

Additionally, JavaScript also includes the modulus operator (%), which returns the remainder of a division operation, and the exponentiation operator (**), which raises a number to a specified power.

+ Operator

The + operator in JavaScript is used for addition when used with two numbers. For example, 2 + 3 would return 5. It can also be used to concatenate strings when used with a string and another type. For example, "Hello " + "world" would return "Hello world". In this case, the + operator acts as a string concatenation operator.

Here's an example of using + Operator in JavaScript


// Addition
alert(2 + 3); // Outputs 5

//Concatenation
alert("Hello" + " world"); //Outputs Hello world
          

- Operator

The - operator is the subtraction operator used for performing arithmetic subtraction between two numbers. It takes two numeric operands and returns the difference of the two numbers. For example, 5 - 3 will return 2.

Here's an example of using - Operator in JavaScript


// Subtraction
alert(10 - 5); // outputs 5
          

* Operator

The * operator in JavaScript is the multiplication operator. It is used to multiply two numbers together and return the result. For example, the expression 3 * 4 would evaluate to 12, because 3 multiplied by 4 is 12.

Here's an example code on how to use * operator in JavaScript

// multiplication
alert(4 * 3); // Outputs 12
          

/ Operator

The / operator is used as the division operator between two numbers. It takes two numbers as operands, divides the first number by the second number, and returns the quotient as the result. For example, the expression 10 / 2 will return the value 5, as 10 divided by 2 is equal to 5.

Here's an example code on how to use / operator in JavaScript


// Division

alert(10 / 2); // Outputs 5
          

% Operator

The % operator is called the modulo operator. It returns the remainder of a division operation between two numbers. For example, if we divide 10 by 3 using the modulus operator, we get the remainder of 1, since 10 divided by 3 equals 3 with a remainder of 1.

Here's an example code on how to use % operator in JavaScript


// Modulo

alert(10 % 3); // Outputs 1
          

** Operator

The "**" operator in JavaScript is the exponentiation operator, also known as the power operator. It is used to raise the left operand to the power of the right operand. For example, 2 ** 3 evaluates to 8, which is equivalent to 2 raised to the power of 3.

Here's an example code on how to use ** operator in JavaScript


// Exponentiation

alert(2 ** 3); // Outputs 8
          

Conclusion

Overall, arithmetic operators in JavaScript are used to perform mathematical operations on numerical values. There are several arithmetic operators available in JavaScript, including addition (+), subtraction (-), multiplication (*), division (/), remainder (%), and exponentiation (**).

Now challenge yourself, click the link below to practice!

  • https://codepen.io/pen/