Operators and Expressions
Learn about operators and expressions in JavaScript to perform operations on variables and values.
Operators and Expressions
In this lesson, we will explore operators and expressions in JavaScript. Understanding how to perform operations on variables and values is crucial for writing effective JavaScript code.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations on numbers.
-
Addition (
+
): Adds two numbers.let sum = 5 + 3; // 8
-
Subtraction (-): Subtracts one number from another.
let difference = 5 - 3; // 2
-
Multiplication (*): Multiplies two numbers.
let product = 5 * 3; // 15
-
Division (/): Divides one number by another.
let quotient = 15 / 3; // 5
-
Modulus (%): Returns the remainder of a division.
let remainder = 5 % 2; // 1
Assignment Operators
Assignment operators are used to assign values to variables.
-
Assignment (=): Assigns a value to a variable.
let x = 10;
-
Addition assignment (+=): Adds a value to a variable and assigns the result to the variable.
let x = 10; x += 5; // x is now 15
-
Subtraction assignment (-=): Subtracts a value from a variable and assigns the result to the variable.
let x = 10; x -= 5; // x is now 5
-
Multiplication assignment (*=): Multiplies a variable by a value and assigns the result to the variable.
let x = 10; x *= 5; // x is now 50
-
Division assignment (/=): Divides a variable by a value and assigns the result to the variable.
let x = 10; x /= 2; // x is now 5
Comparison Operators
Comparison operators are used to compare two values and return a Boolean (true or false).
-
Equal (==): Checks if two values are equal (loose equality).
let isEqual = 5 == "5"; // true
-
Strict equal (===): Checks if two values are equal and of the same type (strict equality).
let isStrictEqual = 5 === "5"; // false
-
Not equal (!=): Checks if two values are not equal (loose inequality).
let isNotEqual = 5 != "5"; // false
-
Strict not equal (!==): Checks if two values are not equal and not of the same type (strict inequality).
let isStrictNotEqual = 5 !== "5"; // true
-
Greater than (>): Checks if the left value is greater than the right value.
let isGreater = 10 > 5; // true
-
Greater than or equal (>=): Checks if the left value is greater than or equal to the right value.
let isGreaterOrEqual = 10 >= 10; // true
-
Less than (<): Checks if the left value is less than the right value.
let isLess = 5 < 10; // true
-
Less than or equal (<=): Checks if the left value is less than or equal to the right value.
let isLessOrEqual = 5 <= 5; // true
Logical Operators
Logical operators are used to combine multiple Boolean expressions.
-
AND (&&): Returns true if both operands are true.
let isTrue = true && true; // true let isFalse = true && false; // false
-
OR (||): Returns true if at least one operand is true.
let isTrue = true || false; // true let isFalse = false || false; // false
-
NOT (!): Returns the opposite Boolean value.
let isNotTrue = !true; // false let isNotFalse = !false; // true
Summary
In this lesson, we covered the various operators and expressions in JavaScript. We explored arithmetic, assignment, comparison, and logical operators. Understanding these operators is essential for performing operations on variables and values in your code.
In the next lesson, we will delve into control structures, learning how to use conditional statements and loops to control the flow of your programs. See you tomorrow!