Site icon Kaizen.Personal computer work.

Arithmetic Operators(JavaScript)

Japanese version.

Arithmetic operators in JavaScript are used to perform arithmetic calculations on numerical values. These operators allow you to add, subtract, multiply, divide, calculate the modulus, and perform exponentiation on numbers.

How to use

+ Addition

The JavaScript addition operator + is a binary operator used to add numeric values. It can also be used for string concatenation.

For numeric addition, you can use it as follows:

let a = 5;
let b = 3;
let c = a + b;

console.log(c); // 8

In this example, we assign the value 5 to the variable a, the value 3 to the variable b, and then use the addition operator + to add them together, assigning the result to the variable c. Finally, we output the value of the variable c to the console.

In addition to adding numbers, the addition operator + can also be used for string concatenation, where it is used to concatenate two or more strings.

let str1 = 'Hello, ';
let str2 = 'World!';
let message = str1 + str2;

console.log(message); // "Hello, World!"

In this example, we assign the string 'Hello, ' to the variable str1 and the string 'World!' to the variable str2, and then use the addition operator + to concatenate them, assigning the result to the variable message. Finally, we output the value of the variable message to the console.

It's important to be careful when using the addition operator + because it is used both for adding numbers and for string concatenation. When you try to add a string and a number, the number is automatically converted to a string and then concatenated with the original string, which can lead to unexpected results.

let x = 5;
let y = '10';
let z = x + y;

console.log(z); // "510"

In this example, we assign the value 5 to the variable x and the string '10' to the variable y, and then use the addition operator + to add them together, assigning the result to the variable z. Because y is a string, the number 5 is converted to a string, and then concatenated with the original string '10', resulting in the string "510".

- Subtraction

The JavaScript subtraction operator - is a binary operator used for numeric subtraction.

Here's an example of using the subtraction operator:

let a = 5;
let b = 3;
let c = a - b;

console.log(c); // 2

In this example, we assign the value 5 to the variable a, the value 3 to the variable b, and then use the subtraction operator - to subtract b from a, assigning the result to the variable c. Finally, we output the value of the variable c to the console.

The subtraction operator - can also be used as a unary operator to negate the value of a variable.

let a = 5;
let b = -a;

console.log(b); // -5

In this example, we assign the value 5 to the variable a, and then use the unary subtraction operator - to negate a, assigning the result to the variable b. The final value of b is -5.

* Multiplication

The JavaScript multiplication operator * is a binary operator used for numeric multiplication.

Here's an example of using the multiplication operator:

let a = 5;
let b = 3;
let c = a * b;

console.log(c); // 15

In this example, we assign the value 5 to the variable a, the value 3 to the variable b, and then use the multiplication operator * to multiply a and b, assigning the result to the variable c. Finally, we output the value of the variable c to the console.

/ Division

The JavaScript division operator / is a binary operator used for numeric division.

Here's an example of using the division operator:

let a = 6;
let b = 3;
let c = a / b;

console.log(c); // 2

In this example, we assign the value 6 to the variable a, the value 3 to the variable b, and then use the division operator / to divide a by b, assigning the result to the variable c. Finally, we output the value of the variable c to the console.

The division operator / cannot be used to divide by zero. When dividing by zero, the result is Infinity or -Infinity.

% Remainder

The JavaScript modulo operator % is a binary operator used for numeric modulo.

Here's an example of using the modulo operator:

let a = 10;
let b = 3;
let c = a % b;

console.log(c); // 1

In this example, we assign the value 10 to the variable a, the value 3 to the variable b, and then use the modulo operator % to calculate the remainder of a divided by b, assigning the result to the variable c. Finally, we output the value of the variable c to the console.

The modulo operator % cannot be used to modulo by zero. When modulo by zero, the result is NaN (Not a Number).

++ Prefix-increment and Postfix-increment

The prefix increment operator ++ in JavaScript is a unary operator that increments the value of a variable by 1, and when applied to a variable, the value of the variable is increased and then returned.

On the other hand, the postfix increment operator ++ in JavaScript is a unary operator that increments the value of a variable by 1, and when applied to a variable, the value of the variable is returned first, and then the value of the variable is increased.

Here is an example code that demonstrates the difference between the prefix and postfix increment operators in JavaScript:

let a = 1;
let b = ++a;
console.log(a); // 2
console.log(b); // 2

let c = 1;
let d = c++;
console.log(c); // 2
console.log(d); // 1

In this example, the initial assignment statements assign the value 1 to variables a and c, respectively. Then, the prefix increment operator ++ is used to increment the value of variable a and assign the result to variable b. On the other hand, the postfix increment operator ++ is used to assign the value of variable c to variable d first, and then increment the value of variable c.

-- Prefix-decrement and Postfix-decrement

The prefix decrement operator -- in JavaScript is a unary operator that decrements the value of a variable by 1, and when applied to a variable, the value of the variable is decreased and then returned.

On the other hand, the postfix decrement operator -- in JavaScript is a unary operator that decrements the value of a variable by 1, and when applied to a variable, the value of the variable is returned first, and then the value of the variable is decreased.

Here is an example code that demonstrates the difference between the prefix and postfix decrement operators in JavaScript:

let a = 2;
let b = --a;
console.log(a); // 1
console.log(b); // 1

let c = 2;
let d = c--;
console.log(c); // 1
console.log(d); // 2

In this example, the initial assignment statements assign the value 2 to variables a and c, respectively. Then, the prefix decrement operator -- is used to decrement the value of variable a and assign the result to variable b. On the other hand, the postfix decrement operator -- is used to assign the value of variable c to variable d first, and then decrement the value of variable c.

** Exponentiation operator

The exponentiation operator in JavaScript is **. The exponentiation operator raises the value of the left operand to the power of the right operand. Here is an example code that demonstrates how to use the exponentiation operator to calculate the power of a number:

let x = 2;
let y = 3;
let result = x ** y;

console.log(result); // 8

In this example, the value 2 is assigned to variable x, the value 3 is assigned to variable y, and the exponentiation operator ** is used to raise the value of variable x to the power of variable y, and the result is assigned to variable result. Therefore, the value of variable result is 8.

---

Links

JavaScript Articles

JavaScript
Exit mobile version