JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



JS Arithmetic

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.

Brief Explanation of JS Arithmetic

JS arithmetic involves performing mathematical operations on numbers. The basic arithmetic operators in JS are:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)

Let's take a look at some code examples to see how these operators work.

Code Examples

To perform arithmetic operations in JS, you can use the basic arithmetic operators mentioned above. Here are some examples:

Addition

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
  

Subtraction

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
  

Multiplication

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
  

Division

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
  

Modulus

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
  

Conclusion

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.

References

Activity