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 = 8Subtraction (-): Used to subtract the right operand from the left operand. Example:
let difference = 10 - 4; // difference = 6Multiplication (*): Used to multiply two operands. Example:
let product = 6 * 7; // product = 42Division (/): Used to divide the left operand by the right operand. Example:
let quotient = 15 / 3; // quotient = 5Modulus (%): Used to get the remainder of the division of the left operand by the right operand. Example:
let remainder = 17 % 5; // remainder = 2Increment (++): Used to increase the value of a variable by 1. Example:
let x = 5; x++; // x = 6Decrement (--): 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 = 8Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand. Example:
let x = 10; x -= 4; // x = 6Multiplication Assignment (*=): Multiplies the left operand with the right operand and assigns the result to the left operand. Example:
let x = 3; x *= 4; // x = 12Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand. Example:
let x = 10; x /= 2; // x = 5Modulus 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); // trueStrict Equal (===): Checks if the two operands are equal in value and type. Example:
let x = 5; let y = '5'; console.log(x === y); // falseNot Equal (!=): Checks if the two operands are not equal in value. Example:
let x = 5; let y = 10; console.log(x != y); // trueStrict 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); // trueGreater Than (>): Checks if the left operand is greater than the right operand. Example:
let x = 10; let y = 5; console.log(x > y); // trueLess Than (<): Checks if the left operand is less than the right operand. Example:
let x = 3; let y = 7; console.log(x < y); // trueGreater 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); // trueLess 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); // trueOR (||): 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); // trueNOT (!): 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); // 10Unary 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.
