Name | Progress |
---|---|
JavaScript for Beginners Introduction | Not Read |
Setting up environment | Not Read |
Name | Progress |
---|---|
Section 1 | Not Read |
Section 2 | Not Read |
Section 3 | Not Read |
Module 1 Quiz | Not Attempted |
Name | Progress |
---|---|
Section 1 | Not Attempted |
Section 2 | Not Attempted |
This lesson focuses on the fundamental arithmetic operations in JavaScript, which are essential for performing calculations within your programs.
Arithmetic Operations
Addition (+): Adds two numbers or concatenates strings.
let sum = 10 + 5;
console.log(sum); // sum is 15
Subtraction (-): Subtracts one number from another.
let difference = 10 - 5;
console.log(difference); // difference is 5
Multiplication (*): Multiplies two numbers.
let product = 10 * 5;
console.log(product); // product is 50
Division (/): Divides one number by another.
let quotient = 10 / 5;
console.log(quotient); // quotient is 2
Modulus (%): Returns the remainder of a division between two numbers.
let remainder = 10 % 3;
console.log(remainder); // remainder is 1
Compound Assignment Operators
Addition Assignment (+=):
let students = 10;
students += 1; // Same as students = students + 1; now students is 11
Subtraction Assignment (-=):
let students = 10;
students -= 1; // Same as students = students - 1; now students is 10
Multiplication Assignment (*=):
let students = 10;
students *= 2; // Same as students = students * 2; now students is 20
Exponentiation Assignment (**=):
let students = 10;
students **= 2; // Same as students = students ** 2; now students is 100
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
Example:
let result = 100 + 50 * 3;
console.log(result); // Expected output: 250, not 450