JavaScript (JS) is a programming language that is widely used in web development. One of the fundamental concepts of JS is arithmetic, which involves performing mathematical operations on numbers. In this article, we will explore the basics of JS arithmetic and provide some code examples to help you understand how it works.
JS arithmetic involves performing mathematical operations on numbers. The basic arithmetic operators in JS are:
Let's take a look at some code examples to see how these operators work.
To perform arithmetic operations in JS, you can use the basic arithmetic operators mentioned above. Here are some examples:
To add two numbers in JS, you can use the addition operator (+). For example:
let num1 = 5;
let num2 = 10;
let sum = num1 + num2;
console.log(sum); // Output: 15
To subtract two numbers in JS, you can use the subtraction operator (-). For example:
let num1 = 10;
let num2 = 5;
let difference = num1 - num2;
console.log(difference); // Output: 5
To multiply two numbers in JS, you can use the multiplication operator (*). For example:
let num1 = 5;
let num2 = 10;
let product = num1 * num2;
console.log(product); // Output: 50
To divide two numbers in JS, you can use the division operator (/). For example:
let num1 = 10;
let num2 = 5;
let quotient = num1 / num2;
console.log(quotient); // Output: 2
The modulus operator (%) returns the remainder of a division operation. For example:
let num1 = 10;
let num2 = 3;
let remainder = num1 % num2;
console.log(remainder); // Output: 1
JS arithmetic is a fundamental concept in web development. By understanding the basic arithmetic operators and how to use them in code, you can perform mathematical operations on numbers in your web applications.