Geek Slack

Introduction to JavaScript
    About Lesson




    JavaScript Arithmetic


    JavaScript Arithmetic

    JavaScript provides several arithmetic operators to perform mathematical operations. These include addition, subtraction, multiplication, division, modulus, increment, and decrement.

    1. Addition

    The addition operator (+) adds two numbers together.

    Example:

    let a = 10;
    let b = 5;
    let sum = a + b;
    
    console.log(sum); // Output: 15

    2. Subtraction

    The subtraction operator (-) subtracts one number from another.

    Example:

    let a = 10;
    let b = 5;
    let difference = a - b;
    
    console.log(difference); // Output: 5

    3. Multiplication

    The multiplication operator (*) multiplies two numbers together.

    Example:

    let a = 10;
    let b = 5;
    let product = a * b;
    
    console.log(product); // Output: 50

    4. Division

    The division operator (/) divides one number by another.

    Example:

    let a = 10;
    let b = 5;
    let quotient = a / b;
    
    console.log(quotient); // Output: 2

    5. Modulus

    The modulus operator (%) returns the remainder when one number is divided by another.

    Example:

    let a = 10;
    let b = 3;
    let remainder = a % b;
    
    console.log(remainder); // Output: 1

    6. Increment

    The increment operator (++) increases a number by one.

    Example:

    let a = 10;
    a++;
    
    console.log(a); // Output: 11

    7. Decrement

    The decrement operator (--) decreases a number by one.

    Example:

    let a = 10;
    a--;
    
    console.log(a); // Output: 9

    8. Exponentiation

    The exponentiation operator (**) raises the first operand to the power of the second operand.

    Example:

    let a = 2;
    let b = 3;
    let power = a ** b;
    
    console.log(power); // Output: 8

    9. Compound Assignment

    JavaScript also supports compound assignment operators that combine an arithmetic operation with assignment.

    Example:

    let a = 10;
    a += 5; // Equivalent to a = a + 5
    a -= 3; // Equivalent to a = a - 3
    a *= 2; // Equivalent to a = a * 2
    a /= 4; // Equivalent to a = a / 4
    a %= 2; // Equivalent to a = a % 2
    
    console.log(a); // Output: 1

    10. Order of Operations

    JavaScript follows the standard order of operations, also known as operator precedence, when evaluating expressions.

    Example:

    let result = 10 + 5 * 2; // Multiplication before addition
    console.log(result); // Output: 20
    
    result = (10 + 5) * 2; // Parentheses change the order of operations
    console.log(result); // Output: 30

    11. Arithmetic with Strings

    When arithmetic operators are used with strings, JavaScript attempts to convert the strings to numbers.

    Example:

    let a = "10";
    let b = "5";
    
    let sum = a + b; // String concatenation
    console.log(sum); // Output: "105"
    
    let difference = a - b; // Subtraction with conversion to numbers
    console.log(difference); // Output: 5
    
    let product = a * b; // Multiplication with conversion to numbers
    console.log(product); // Output: 50
    
    let quotient = a / b; // Division with conversion to numbers
    console.log(quotient); // Output: 2

    By understanding and using these arithmetic operators, you can perform a wide range of mathematical operations in JavaScript.