# Operators in Programming – Notes from Class (plus my own confusion)

* * *

Alright picking up from where I left off last time. Today's topic is operators. And honestly this one felt pretty easy at first because like... we've been doing math since class 5. But then the comparison operators showed up and I had a small crisis. More on that in a bit.

* * *

**So What Are Operators?**

An operator is basically a symbol that tells the program to do something with one or more values. That's it. When you write 5 + 3, the + is the operator. It's operating on the values 5 and 3 and giving you back 8.

The values that operators work on are called operands. So in 5 + 3, the operands are 5 and 3, and + is the operator. You probably won't need to remember that word for anything practical but teachers love asking it in exams so.

There are different types of operators depending on what they do. Let's go through them one by one.

* * *

**Arithmetic Operators**

These are the math ones. Most of them you already know.

\+ adds two numbers - subtracts \* multiplies / divides % this one is called modulus and it gives you the remainder after division

Examples:

let a = 10; let b = 3;

console.log(a + b); // 13 console.log(a - b); // 7 console.log(a \* b); // 30 console.log(a / b); // 3.333... console.log(a % b); // 1

The only one that might be new is %. So 10 divided by 3 is 3 with a remainder of 1. The % operator just gives you that remainder. It sounds useless but it's actually super handy. Like if you want to check if a number is even or odd you do number % 2 and if the result is 0 it's even, if it's 1 it's odd. You'll use it more than you think.

* * *

**Comparison Operators**

Okay here's where it gets interesting. Comparison operators are used to compare two values. The result of any comparison is always either true or false. That's it. No other possibilities.

\== checks if two values are equal != checks if two values are NOT equal

> checks if left side is greater than right side < checks if left side is less than right side = greater than or equal to <= less than or equal to

console.log(5 > 3); // true console.log(5 < 3); // false console.log(5 == 5); // true console.log(5 != 3); // true

Simple enough right. But now here's the part that got me.

* * *

**<mark class="bg-yellow-200 dark:bg-yellow-500/30">vs </mark> \= and I Cannot Stress This Enough**

This is specifically a JavaScript thing and it broke my brain a little when I first saw it.

<mark class="bg-yellow-200 dark:bg-yellow-500/30">is called the equality operator and </mark> \= is called the strict equality operator.

The difference is this:

\== compares only the VALUE. It doesn't care about the data type. So if needed it will convert one value to match the other type before comparing. This is called type coercion.

\=== compares both the VALUE and the DATA TYPE. No conversion happens. It's a stricter check.

Look at this:

console.log(5 <mark class="bg-yellow-200 dark:bg-yellow-500/30">"5"); // true console.log(5 </mark> \= "5"); // false

Wait what? How is 5 == "5" true when one is a number and one is a string?

Because <mark class="bg-yellow-200 dark:bg-yellow-500/30">sees that 5 and "5" have the same value if you ignore the type, so it says okay close enough, true. But </mark> \= looks at both and says – hold on, one is a number and one is a string, these are not the same thing, false.

Another example:

console.log(0 <mark class="bg-yellow-200 dark:bg-yellow-500/30">false); // true console.log(0 </mark> \= false); // false

0 and false are technically the same in terms of value because in JavaScript false is treated as 0. So <mark class="bg-yellow-200 dark:bg-yellow-500/30">says true. But </mark> \= says no because 0 is a number and false is a boolean, different types, false.

My teacher said just use === by default most of the time. It avoids weird surprises. Use == only if you specifically want type conversion to happen, which is rare. Good advice honestly.

Same thing applies to != vs !==. != ignores type, !== is strict.

* * *

**Logical Operators**

Logical operators are used to combine conditions. You'll mostly see these inside if statements.

There are three:

&& means AND – both conditions must be true for the result to be true || means OR – at least one condition must be true for the result to be true ! means NOT – it flips the result, true becomes false and false becomes true

Let me show examples:

let age = 20; let hasID = true;

console.log(age >= 18 && hasID); // true (both are true) console.log(age >= 18 || hasID); // true (at least one is true) console.log(!hasID); // false (flips true to false)

Let's say age was 15:

console.log(age >= 18 && hasID); // false (first condition is false, so whole thing is false) console.log(age >= 18 || hasID); // true (second condition is still true, so result is true)

The way I remember it – AND is strict, both sides have to agree. OR is chill, just one side needs to be happy. And NOT just flips whatever you give it.

Here's a truth table I made in my notes for all three:

AND (&&) true && true = true true && false = false false && true = false false && false = false

OR (||) true || true = true true || false = true false || true = true false || false = false

NOT (!) !true = false !false = true

The OR one is important to remember – it's only false when BOTH sides are false. Any one side being true makes the whole thing true.

* * *

**Assignment Operators**

You already know = right. It assigns a value to a variable. let x = 5 means put 5 into x. That's the basic assignment operator.

But there are shorthand ones too:

+= adds to the current value and assigns back -= subtracts from current value and assigns back \*= multiplies current value and assigns back /= divides current value and assigns back %= modulus and assigns back

Example:

let x = 10;

x += 5; // same as x = x + 5, so x is now 15 x -= 3; // same as x = x - 3, so x is now 12 x *\= 2; // same as x = x* 2, so x is now 24 x /= 4; // same as x = x / 4, so x is now 6 x %= 4; // same as x = x % 4, so x is now 2

These are just shortcuts. You don't HAVE to use them but they make the code shorter and once you get used to them they feel natural. x += 1 is much cleaner than writing x = x + 1 every time.

* * *

**Assignment**

*Task 1 – Arithmetic Operations on Two Numbers*

let num1 = 20; let num2 = 6;

console.log("Addition: " + (num1 + num2)); // 26 console.log("Subtraction: " + (num1 - num2)); // 14 console.log("Multiplication: " + (num1 \* num2)); // 120 console.log("Division: " + (num1 / num2)); // 3.333... console.log("Remainder: " + (num1 % num2)); // 2

* * *

*Task 2 – Comparing with <mark class="bg-yellow-200 dark:bg-yellow-500/30">and </mark> \=*

let val1 = 10; let val2 = "10";

console.log(val1 <mark class="bg-yellow-200 dark:bg-yellow-500/30">val2); // true (same value, type ignored) console.log(val1 </mark> \= val2); // false (different types, number vs string)

let val3 = 10; console.log(val1 <mark class="bg-yellow-200 dark:bg-yellow-500/30">val3); // true console.log(val1 </mark> \= val3); // true (same value AND same type)

* * *

*Task 3 – Condition Using Logical Operators*

let score = 72; let attendance = 80;

if (score >= 40 && attendance >= 75) { console.log("Eligible to appear for final exam"); } else { console.log("Not eligible – check score and attendance"); }

I used && here because both conditions need to be true at the same time. Score has to be passing AND attendance has to be enough. If either one fails, the student shouldn't be eligible. So AND made more sense than OR here.

* * *

**Quick Recap**

*   Operators are symbols that perform operations on values
    
*   Arithmetic operators do math – +, -, \*, /, %
    
*   Comparison operators compare values and return true or false – <mark class="bg-yellow-200 dark:bg-yellow-500/30">, </mark> \=, !=, >,
    
*   <mark class="bg-yellow-200 dark:bg-yellow-500/30">checks value only, </mark> \= checks value AND type – prefer === in JavaScript
    
*   Logical operators combine conditions – && needs both true, || needs one true, ! flips the result
    
*   Assignment operators are shortcuts for updating a variable – +=, -=, \*=, etc.
    

That's honestly most of what you need for now. Operators are everywhere in code. Every condition you write, every calculation, every loop – it all uses these. So getting comfortable with them early makes everything else easier later.

Next up I think we're doing loops. Looking forward to it and also slightly dreading it.
