DeveloperBreeze

Arithmetic Operators

Section 2.3: Operators

Tutorial 2.3.1: Arithmetic Operators


Arithmetic Operators in JavaScript

Arithmetic operators are used to perform mathematical calculations on numeric values (literals or variables). They are fundamental for tasks ranging from simple calculations to complex algorithms.


OperatorSymbolDescriptionExampleResult
Addition+Adds two numbers5 + 38
Subtraction-Subtracts one number from another10 - 46
Multiplication*Multiplies two numbers7 * 642
Division/Divides one number by another20 / 45
Modulus%Returns the remainder of division10 % 31
Exponentiation**Raises a number to a power2 ** 38
Increment++Increases a number by 1let x = 5; x++6
Decrement--Decreases a number by 1let y = 7; y--6

List of Arithmetic Operators

  1. Addition (+):
  • Adds two numbers.
  • Example:
     let sum = 10 + 5; // 15
  1. Subtraction (-):
  • Subtracts the second number from the first.
  • Example:
     let difference = 10 - 5; // 5
  1. Multiplication (*):
  • Multiplies two numbers.
  • Example:
     let product = 10 * 5; // 50
  1. Division (/):
  • Divides the first number by the second.
  • Example:
     let quotient = 10 / 5; // 2
  1. Modulus (%):
  • Returns the remainder of the division.
  • Example:
     let remainder = 10 % 3; // 1
  1. Exponentiation (</strong>)**:
  • Raises the first operand to the power of the second operand.
  • Example:
     let power = 2 ** 3; // 8
  1. Increment (++):
  • Increases a variable by one.
  • Postfix Increment (i++):
     let i = 5;
     console.log(i++); // Outputs 5, then i becomes 6
     console.log(i);   // Outputs 6
  • Prefix Increment (++i):
     let i = 5;
     console.log(++i); // Outputs 6
  1. Decrement (--):
  • Decreases a variable by one.
  • Postfix Decrement (i--):
     let i = 5;
     console.log(i--); // Outputs 5, then i becomes 4
     console.log(i);   // Outputs 4
  • Prefix Decrement (--i):
     let i = 5;
     console.log(--i); // Outputs 4

Operator Precedence

Operator precedence determines the order in which operations are performed in an expression.

  • Order of Operations:
  1. Parentheses ()
  2. Exponentiation **
  3. Multiplication *, Division /, Modulus %
  4. Addition +, Subtraction -
  • Example:
  let result = 10 + 5 * 2; // 20
  // Multiplication happens before addition
  // 10 + (5 * 2) = 10 + 10 = 20

  let resultWithParentheses = (10 + 5) * 2; // 30
  // Parentheses alter the order
  // (10 + 5) * 2 = 15 * 2 = 30

Type Coercion with the `+` Operator

  • When adding a number and a string, JavaScript converts the number to a string and concatenates.
  let message = "The total is: " + 20; // "The total is: 20"
  • Be cautious to avoid unexpected results.
  console.log(5 + "5"); // "55"
  console.log(5 - "2"); // 3 (string "2" is coerced to number)

Unary Operators

  1. Unary Plus (+):
  • Attempts to convert the operand to a number.
  • Example:
     let x = "5";
     let num = +x; // 5 (number)
  1. Unary Negation (-):
  • Converts the operand to a number and negates it.
  • Example:
     let x = "5";
     let num = -x; // -5 (number)

Examples of Arithmetic Operations

  1. Calculating the Area of a Circle:
   const PI = 3.1416;
   let radius = 5;
   let area = PI * radius ** 2;
   console.log("Area:", area); // Area: 78.54
  1. Even or Odd Checker:
   let number = 7;
   if (number % 2 === 0) {
     console.log(number + " is even.");
   } else {
     console.log(number + " is odd."); // Outputs: 7 is odd.
   }
  1. Incrementing Values in a Loop:
   for (let i = 0; i < 5; i++) {
     console.log("Count:", i);
   }
   // Outputs:
   // Count: 0
   // Count: 1
   // Count: 2
   // Count: 3
   // Count: 4

Common Mistakes to Avoid

  • Integer Division:
  • Unlike some languages, dividing integers in JavaScript returns a floating-point number.
    console.log(5 / 2); // 2.5
  • Modulus with Negative Numbers:
  • Be cautious, as the result can be negative.
    console.log(-10 % 3); // -1

Exercise

  1. Simple Calculator:
  • Create variables a and b with values 12 and 4.
  • Perform all arithmetic operations and log the results.
     let a = 12;
     let b = 4;
     console.log("Addition:", a + b);        // 16
     console.log("Subtraction:", a - b);     // 8
     console.log("Multiplication:", a * b);  // 48
     console.log("Division:", a / b);        // 3
     console.log("Modulus:", a % b);         // 0
  1. Increment and Decrement Practice:
  • Initialize counter to 10.
  • Use both prefix and postfix increment/decrement operators and observe the outputs.
     let counter = 10;
     console.log(counter++); // 10
     console.log(counter);   // 11
     console.log(++counter); // 12
     console.log(counter--); // 12
     console.log(counter);   // 11
     console.log(--counter); // 10

Related Posts

More content you might like

Tutorial
javascript

Comparison and Logical Operators

console.log(5 == "5"); // true (type coercion)
console.log(5 === "5"); // false (no type coercion)
console.log(10 > 7); // true
console.log(4 <= 4); // true

Logical operators combine multiple conditions or values.

Dec 11, 2024
Read More
Tutorial
javascript

Non-Primitive Data Types (Objects, Arrays, and Functions)

  person.city = "New York";
  person.age = 26;
  console.log(person);
  • Deleting Properties:

Dec 11, 2024
Read More
Tutorial
javascript

Primitive Data Types

    console.log("abc" * 2); // NaN

Used for very large integers beyond the safe range of number.

Dec 11, 2024
Read More
Tutorial
javascript

Variables and Constants

     function test() {
       var y = 20;
       console.log(y); // 20
     }
     console.log(y); // Error: y is not defined
let name = "Alice";
console.log(name);

const BIRTH_YEAR = 1990;
// BIRTH_YEAR = 2000; // Error: Assignment to constant variable

Dec 10, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!