JavaScript Comparisons & Booleans¶
What are Arithmetic Operators?¶
Arithmetic operators are used to perform mathematical calculations like addition, subtraction, and multiplication. They are the most basic building blocks of programming logic.
| Operator | Operation | Description |
|---|---|---|
+ |
Addition | Adds two values together |
- |
Subtraction | Subtracts the right value from the left value |
* |
Multiplication | Multiplies two values |
/ |
Division | Divides the left value by the right value |
% |
Modulus | Returns the remainder after division |
Note: While the basic behavior is the same across languages, how they handle things like "Integer Division" (e.g., 5 / 2) can vary.
Comparison Operators¶
| Operator | Description |
|---|---|
== |
equal to (value only) |
=== |
equal value and equal type |
!= |
not equal |
!== |
not equal value or not equal type |
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
? |
ternary operator |
Strict Equality (===)¶
In JavaScript, == converts variable values to the same type before comparing. This is called Type Coercion.
=== does not convert type.
let x = 5;
x == "5"; // true (string "5" converted to number 5)
x === "5"; // false (number vs string)
Logical Operators¶
&&: logical and||: logical or!: logical not
Conditional (Ternary) Operator¶
JavaSript also contains a conditional operator that assigns a value to a variable based on some condition.
Syntax: variablename = (condition) ? value1:value2
Always use Strict Equality
To avoid bugs, always use === and !== instead of == and !=. It makes your code more predictable.