Arithmetic Operations
by Louis Nguyen· Module 2: Introduction to Variables and Data Types

Root Folder
Not Attempted
NameProgress
JavaScript for Beginners Introduction
Not Read
Setting up environment
Not Read
Module 1: Engaging with JavaScript Fundamentals
Not Attempted
NameProgress
Section 1
Not Read
Section 2
Not Read
Section 3
Not Read
Module 1 Quiz
Not Attempted
Section 1
Arithmetic Operations
Type Conversion
Module 3
Not Attempted
NameProgress
Section 1
Not Attempted
Section 2
Not Attempted
0 / 30 XP

This lesson focuses on the fundamental arithmetic operations in JavaScript, which are essential for performing calculations within your programs.

Arithmetic Operations

  1. Addition (+): Adds two numbers or concatenates strings.

    let sum = 10 + 5;  
    console.log(sum);  // sum is 15
  2. Subtraction (-): Subtracts one number from another.

    let difference = 10 - 5;  
    console.log(difference);  // difference is 5
  3. Multiplication (*): Multiplies two numbers.

    let product = 10 * 5;  
    console.log(product);  // product is 50
  4. Division (/): Divides one number by another.

    let quotient = 10 / 5;  
    console.log(quotient);  // quotient is 2
  5. Modulus (%): Returns the remainder of a division between two numbers.

    let remainder = 10 % 3;  
    console.log(remainder);  // remainder is 1

Compound Assignment Operators

  1. Addition Assignment (+=):

    let students = 10;
    students += 1;  // Same as students = students + 1; now students is 11
  2. Subtraction Assignment (-=):

    let students = 10;
    students -= 1;  // Same as students = students - 1; now students is 10
  3. Multiplication Assignment (*=):

    let students = 10;
    students *= 2;  // Same as students = students * 2; now students is 20
    
  4. Exponentiation Assignment (**=):

    let students = 10;
    students **= 2; // Same as students = students ** 2; now students is 100
  5. Modulus Assignment (%=):

    let students = 10;
    students %= 3;  // Same as students = students % 3; now students is 1

Increment and Decrement Operators

These operators increase or decrease a variable's value by one, which is very common in loops and conditional statements.

  • Increment (++)

    students++;  // Increment students by 1
  • Decrement (--)

    students--;  // Decrement students by 1

Operator Precedence

  • Understanding operator precedence is crucial for predicting how expressions are evaluated. In JavaScript, operations are generally performed from left to right, with multiplication, division, and modulus operations being executed before addition and subtraction.
  • Example:

    let result = 100 + 50 * 3;
    console.log(result);  // Expected output: 250, not 450
Maggie

Discuss with Maggie
Use the power of generative AI to interact with course content

Discussion

0 comments
Loading editor ...
Remember to be polite and report any undesirable behaviour

Category

Empty

Labels

Discussion has no labels

1 participant

user avatar

Priority

Notifications

You're not receiving notifications from this thread.
Course Outline
Module 1: Engaging with JavaScript Fundamentals
Module 2: Introduction to Variables and Data Types
Module 3
Module 4
Module 5