Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
3 min read
P
Since I am developer. My blogs are related to Tech and also blogs write during web dev cohort by chai code.

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 % 3 is 1, because 3 goes into 10 three times with 1 left over.

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 += 5 is the same as saying x = 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).

Truth table for logical operators AND OR NOT, AI generated

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:

  1. Math Time: Create two variables a = 15 and b = 4. Find the remainder when a is divided by b.

  2. The Secret Equality: Compare 10 == "10" and 10 === "10". Why does JavaScript give you different answers?

  3. The Gatekeeper: Create a variable age = 20 and hasTicket = true. Write a single line of code using && that checks if the person can enter a concert (must be 18+ and have a ticket).

  4. Shortcut: Use the += operator to increase your age by 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!

Web Development

Part 23 of 34

This series is part of my journey with "Chai aur Code" Web Dev Cohort. I have written blogs on various topics, and it is still going. So, Enjoy My Blogs on Web Development.

Up next

Understanding the this Keyword in JavaScript

this is little bit confusing but today we will try to make it easy. this at the top when we use this directly. In browser it points to the window object. like if you want you can do this.alert() this