Basic JavaScript Operators
JavaScript operators are symbols that perform specific operations on one or more operands (values, variables, etc.) and return a result. These operators are essential for writing effective and efficient code in JavaScript. Let's explore the basic JavaScript operators:
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations. The basic arithmetic operators in JavaScript are:
-
Addition (+): Used to add two operands.
Example:let sum = 5 + 3; // sum = 8
-
Subtraction (-): Used to subtract the right operand from the left operand.
Example:let difference = 10 - 4; // difference = 6
-
Multiplication (*): Used to multiply two operands.
Example:let product = 6 * 7; // product = 42
-
Division (/): Used to divide the left operand by the right operand.
Example:let quotient = 15 / 3; // quotient = 5
-
Modulus (%): Used to get the remainder of the division of the left operand by the right operand.
Example:let remainder = 17 % 5; // remainder = 2
-
Increment (++): Used to increase the value of a variable by 1.
Example:let x = 5; x++; // x = 6
-
Decrement (--): Used to decrease the value of a variable by 1.
Example:let y = 10; y--; // y = 9
Assignment Operators
Assignment operators are used to assign values to variables. The basic assignment operator is the equal sign (=
), but there are also compound assignment operators that combine an arithmetic or logical operation with the assignment.
-
Equal (=): Assigns the value of the right operand to the left operand.
Example:let x = 5;
-
Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
Example:let x = 5; x += 3; // x = 8
-
Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
Example:let x = 10; x -= 4; // x = 6
-
Multiplication Assignment (*=): Multiplies the left operand with the right operand and assigns the result to the left operand.
Example:let x = 3; x *= 4; // x = 12
-
Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
Example:let x = 10; x /= 2; // x = 5
-
Modulus Assignment (%=): Divides the left operand by the right operand and assigns the remainder to the left operand.
Example:let x = 7; x %= 3; // x = 1
Comparison Operators
Comparison operators are used to compare two values and return a boolean value (true or false).
-
Equal (==): Checks if the two operands are equal in value.
Example:let x = 5; let y = '5'; console.log(x == y); // true
-
Strict Equal (===): Checks if the two operands are equal in value and type.
Example:let x = 5; let y = '5'; console.log(x === y); // false
-
Not Equal (!=): Checks if the two operands are not equal in value.
Example:let x = 5; let y = 10; console.log(x != y); // true
-
Strict Not Equal (!==): Checks if the two operands are not equal in value and type.
Example:let x = 5; let y = '5'; console.log(x !== y); // true
-
Greater Than (>): Checks if the left operand is greater than the right operand.
Example:let x = 10; let y = 5; console.log(x > y); // true
-
Less Than (<): Checks if the left operand is less than the right operand.
Example:let x = 3; let y = 7; console.log(x < y); // true
-
Greater Than or Equal To (>=): Checks if the left operand is greater than or equal to the right operand.
Example:let x = 7; let y = 7; console.log(x >= y); // true
-
Less Than or Equal To (<=): Checks if the left operand is less than or equal to the right operand.
Example:let x = 5; let y = 10; console.log(x <= y); // true
Logical Operators
Logical operators are used to combine or invert boolean values.
-
AND (&&): Returns true if both operands are true, otherwise, it returns false.
Example:let x = 5; let y = 10; console.log(x > 3 && y > 7); // true
-
OR (||): Returns true if at least one of the operands is true, otherwise, it returns false.
Example:let x = 5; let y = 10; console.log(x > 7 || y > 7); // true
-
NOT (!): Inverts the value of the operand. If the operand is true, it returns false, and if the operand is false, it returns true.
Example:let x = true; console.log(!x); // false
Other Operators
-
Conditional (Ternary) Operator (?:): Used as a shorthand for an if-else statement.
Example:let age = 18; let canVote = age >= 18 ? 'Yes' : 'No'; // canVote = 'Yes'
-
Typeof Operator: Returns the data type of the operand.
Example:let x = 5; console.log(typeof x); // 'number'
-
Comma Operator (,): Evaluates each operand from left to right and returns the value of the last operand.
Example:let x = (5, 10); console.log(x); // 10
-
Unary Operators: Operate on a single operand, such as increment (++), decrement (--), and the unary plus (+) and minus (-).
Example:let x = 5; console.log(++x); // 6
Understanding these basic JavaScript operators will help you write more efficient and expressive code. Remember to practice and experiment with these operators to solidify your understanding.