codequick-darkmode-logo
ConnexionS'inscrire

Basic Math Operations in JavaScript

JavaScript provides a variety of built-in math operations that allow you to perform numerical computations in your web applications. In this article, we will explore the basic math operations available in JavaScript and how to use them effectively.

Addition (+)

The addition operator, represented by the plus sign (+), is used to add two or more numbers together. Here's an example:

let sum = 2 + 3; console.log(sum); // Output: 5

Additionally, the plus sign (+) can also be used for string concatenation, where it concatenates two string values together. Here's an example:

let name = "John"; let greeting = "Hello " + name; console.log(greeting); // Output: Hello John

Subtraction (-)

The subtraction operator, represented by the minus sign (-), is used to subtract one number from another. Here's an example:

let difference = 7 - 4; console.log(difference); // Output: 3

Multiplication (*)

The multiplication operator, represented by the asterisk (*), is used to multiply two numbers together. Here's an example:

let product = 5 * 6; console.log(product); // Output: 30

Division (/)

The division operator, represented by the forward slash (/), is used to divide one number by another. Here's an example:

let quotient = 15 / 3; console.log(quotient); // Output: 5

It's worth noting that when dividing two integers, JavaScript will return a floating-point number as the result.

Modulo (%)

The modulo operator, represented by the percent sign (%), is used to find the remainder of the division between two numbers. Here's an example:

let remainder = 10 % 4; console.log(remainder); // Output: 2

The modulo operator is often used to perform tasks such as determining whether a number is even or odd.

These basic math operations can be combined and used within larger JavaScript expressions. It's important to ensure proper order of operations by using parentheses to group sub-expressions when necessary.

Additional Resources

For more information on JavaScript math operations, refer to the following resources: