JavaScript Operators: The Basics You Need to Know
What operators are ?
Operators are special symbols or keywords that perform operations on variables and values (operands) .They are the building blocks of expressions and are used to manipulate data, compare values, and control program flow. Operators are also in math like multiplication *, division /, addition+, and subtraction -. They are also in Javascript. They are of several types:-
Arithmetic operators (+, -, *, /, %)
Comparison operators (==, ===, !=, >, <)
Logical operators (&&, ||, !)
Assignment operators (=, +=, -=)
Arithmetic operators (+, -, *, /, %)
Arithmetic operators in JavaScript perform mathematical calculations on numerical values (operands). The primary arithmetic operators are:-
1 + Addition :- Adds two operands. This operator can also concatenate strings if one or both operands are strings.
Example:- 5+3=8 and Hello+ World=HelloWorld
2 - Subtraction:- Subtracts the right operand from the left operand.
Example:- 10-6=4
3 * Multiplication:- Multiplies two operands.
Example:- 6*7= 42
4 / Division:- Divides the left operand by the right operand. Division by zero results Infinity.
Example 6/3= 2
5 % Modulus or Remainder:- Returns the remainder of a division operation. It's useful for tasks like determining if a number is even or odd.
Example:- 10%3=1
Comparison operators (==, ===, !=, >, <)
1 Loose Equality ==:- It compares the values of two operands, performing type conversion if they are of different types. Example 5=="5" returns true
2 Strict Equality=== :- It compares both the value and the data type of the operands without any type conversion. Example 5==="5"
3 Inequality!= :- This operator checks if two values are not equal, with type coercion. It returns true if they are different and false if they are same. Example 5!= 6 true and 5!= "5" false
4 Greater than > :- This checks if the left operand is greater than the right operand.
Example 6>5 true
5 Less than < :- This checks if the left operand is less than the right operand.
Example 2<3 true
Logical operators (&&, ||, !)
Logical AND && :- The logical AND operator is used when you want two or more conditions must be true at same time. It checks multiple conditions together and gives a true result only if every condition is true. Like:- “You must complete homework AND bring your notebook.”
Logical OR || :- The logical OR operator is used when you want at least one condition true. It checks multiple conditions and gives a true result when any one condition is true. Like:- “You can enter if you have a ticket OR you are a VIP.”
Logical OR ! :- The logical NOT operator is used to reverse the value of condition. It changes true into false and false into true.
Assignment operators (=, +=, -=)
= Simple Assignment :- Assigns the value from the right-hand side to the variable on the left.
Example:- x=10
+= Addition Assignment :- Adds the right-hand operand to the left-hand operand and assigns the result to the left-hand operand.
Example:- x += 5 is equivalent to x=x-3
-= Subtraction Assignment :- Subtracts the right-hand operand from the left-hand operand and assigns the result to the left-hand operand.
Example:- x-= 3 is equivalent to x = x-3