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

  • == performs type coercion.
  • === ensures both value and type match.
  • Logical operators evaluate left to right; ensure your conditions are correct.

Dec 11, 2024
Read More
Tutorial
javascript

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

  • Iterating Over Arrays:
  for (let fruit of fruits) {
    console.log(fruit);
  }

Dec 11, 2024
Read More
Tutorial
javascript

Primitive Data Types

  let isLoggedIn = true;
  let hasAccess = false;
  console.log(isLoggedIn && hasAccess); // false

A variable that has been declared but not assigned a value is undefined.

Dec 11, 2024
Read More
Tutorial
javascript

Variables and Constants

  • Use const for constants or variables that should remain unchanged.
  • Example:
     const API_KEY = "12345";
     // API_KEY = "67890"; // 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!