Introduction
In JavaScript, operators are symbols or keywords used to perform operations on values and variables. Whether you're working with numbers, checking conditions, or making comparisons, JavaScript operators help you manage logic and control flow effectively.
As a beginner, understanding how these operators work will help you write better, cleaner, and more effective code. Our goal with this guide is to make learning JavaScript operators easy, clear, and accessible for beginners.
In the article, we'll cover the following areas:
- Arithmetic Operators (+, - ,* , /, %)
- Assignment Operators (=, +=, -=, etc.)
- Comparison Operators (==, ===, !=, !==, <, >, <=, >=)
- Logical Operators (&&, ||, !)
- Ternary Operator (
condition? value_if_true: value_if_false
) - Type Operators ('typeof', 'instanceof')
Let’s now go through each type one by one.
Arithmetic Operators
These operators allow you to perform mathematical operations on numbers.
Addition (+): This operator is commonly used to add one value to another.
Example:
let output = 5 + 3; // output is 8
Subtraction (-): Subtracts one value from another.
Example:
let output = 5 - 3; // output is 2
Multiplication (*): Multiplies two values.
Example:
let output = 5 * 3; // output is 15
Division ( / ): Divides one value by another.
Example:
let output = 6 / 3; // output is 2
Modulus (%): Returns the leftover part of a division between two values.
Example:
let output = 5 % 3; // output is 2
Assignment Operators
These are used for setting or updating variable values. They can also perform operations during an assignment.
Simple Assignment (=): In JavaScript, the equals sign (=) is called the assignment operator.
Example:
let a = 5; // a is now 5
Addition Assignment Operator (+=): Updates the variable by adding another value to it.
Example:
a += 3; // a is now 8 (a = a + 3)
Subtraction Assignment Operator (-=): Subtracts a value from the existing value.
Example:
a -= 2; // a is now 6 (a = a - 2)
Comparison Operators
Comparison operators test values against each other and produce a true or false outcome.
Equality Operator (==): Checks if two values are equal (loosely).
Example:
5 == '5'; // returns true because it only compares values, not types
Strict Equality Operator (===): Checks if two values are equal and of the same type (strict comparison).
Example:
5 === '5'; // returns false because the types are different (number vs. string)
Inequality Operator (!=): Checks if two values are not equal (loosely).
Example:
5 != '5'; // returns false because it only compares values
Strict Inequality Operator (!==): Checks if two values are not equal or of the same type.
Example:
5 !== '5'; // returns true because they are of different types
<, >, <=, >=
: Checks if one value is less than, greater than, less than or equal to, or greater than or equal to another value.
Example:
5 < 10; // returns true
5 >= 10; // returns false
Logical Operators
In programming, logical operators are tools for making decisions using Boolean values such as true and false.
AND (&&): The && operator evaluates to true only when both conditions are true.
Example:
true && false; // returns false
OR ( || ): With OR, only one of the conditions needs to be true for the whole statement to be true.
Example:
true || false; // returns true
NOT (!): The Not operator inverts the Boolean value; !true becomes false.
Example:
!true; // returns false
Ternary Operator
This operator allows you to simplify if-else logic into a single line with three elements: condition, true result, and false result.
Example:
let age = 20;
let canVote = age >= 20 ? 'Yes': 'No'; // canVote will be 'Yes' because the condition is true
Type Operators
These operators are used to check types and instances.
typeof:
Returns the type of a variable or value.
Example:
typeof 'Hello'; // returns 'string'
typeof 5; // returns 'number
instanceof
: checks whether a given object is of a specified class.
Example:
let person = new Person();
person instanceof Person; // returns true
Conclusion
Operators in JavaScript are fundamental building blocks that help you perform actions on variables and data. Whether you're doing calculations, making comparisons, or testing conditions, mastering operators will make your code more powerful and efficient. Start practicing with these operators and soon you’ll be able to apply them confidently in your projects!
You can reach out to me via LinkedIn
Top comments (0)