JavaScript Operators: The Basics You Need to Know
In programming, Operators are the symbols that tell the computer to perform specific actions on your data. If variables are the nouns (the things) and functions are the verbs (the actions), then operators are the mathematical and logical "glue" that connects them.
Arithmetic Operators (The Math)
These are exactly like the math you learned in school, with one special addition.
+(Addition)-(Subtraction)*(Multiplication)/(Division)%(Remainder/Modulo): This gives you what is "left over" after a division.- Example:
10 % 3is 1, because 3 goes into 10 three times with 1 left over.
- Example:
Assignment Operators (Storing Values)
We use these to put values into variables.
=: Standard assignment (let x = 10).+=/-=: A shortcut to update a variable based on its current value.x += 5is the same as sayingx = x + 5.
Comparison Operators (The Great Debaters)
These operators always return a Boolean (true or false). They are used to compare two values.
>(Greater than) /<(Less than)==(Equal to): Checks if the values are the same, even if the "type" is different (e.g.,5 == "5"is true).===(Strictly Equal to): Checks if both the value and the type are the same. This is the one you should almost always use!
console.log(5 == "5"); // true
console.log(5 === "5"); // false (because a number is not a string)
Logical Operators (Connecting Ideas)
These help you combine multiple conditions.
&&(AND): Returns true only if both sides are true.||(OR): Returns true if at least one side is true.!(NOT): Flips the value (true becomes false, false becomes true).
Summary Table
Category | Operators | Best Use Case |
Arithmetic |
| Basic calculations. |
Assignment |
| Updating scores or counts. |
Comparison |
| Checking if a user is old enough or has enough money. |
Logical |
|
Practice Assignment
Let's see if your logic is sharp! Try these in your console:
Math Time: Create two variables
a = 15andb = 4. Find the remainder whenais divided byb.The Secret Equality: Compare
10 == "10"and10 === "10". Why does JavaScript give you different answers?The Gatekeeper: Create a variable
age = 20andhasTicket = true. Write a single line of code using&&that checks if the person can enter a concert (must be 18+ and have a ticket).Shortcut: Use the
+=operator to increase yourageby 5.
Pro-Tip: Remember that ! (NOT) is like a "reverse" button. !true is false. It's incredibly useful for toggling things on and off, like a "Dark Mode" setting!




