Arithmetic operators#
22 12 85 3.4 2 1024 16 5 4 5 9
n % 2 === 0 tells you if a number is even - one of the most used checks in programming.
Comparison operators#
| Operator | Meaning | 5 ? 3 |
|---|---|---|
=== |
equal (value and type) | false |
!== |
not equal | true |
> < >= <= |
greater / less | true, false, true, false |
==, != |
loose equality - converts types first | avoid |
true false true false true false true
Always use === and !==. Loose == gives surprising results like "" == 0 being true.
Logical operators#
| Operator | Meaning | Example |
|---|---|---|
&& |
AND - both must be true | age >= 18 && hasId |
|| |
OR - at least one true | isAdmin || isTeacher |
! |
NOT - flips true/false | !isBlocked |
Truthy and falsy#
In a condition, every value acts as true or false. These six are falsy: false, 0, "", null, undefined, NaN. Everything else is truthy.
Please enter your name true true false
if / else if / else#
Hot day - drink water.
if से program फैसला लेता है। ऊपर से नीचे हर condition check होती है, और जो पहली सही (true) निकलती है उसी का block चलता है - बाकी छोड़ दिए जाते हैं। कोई भी सही न हो तो else चलता है।
The ternary operator#
A short if/else that returns a value:
Result: Fail You have 1 item in your cart
switch#
Good when one value is compared with many fixed options:
Weekend batch: 10 AM
Don't forget break - without it, the code "falls through" into the next case.
?? and ?. - safe defaults#
100 0 undefined City not given
Summary#
- Arithmetic:
+ - * / % **; shortcuts+=,++. - Compare with
===/!==; combine with&&,||,!. - Falsy:
false, 0, "", null, undefined, NaN. if / else if / else, the ternaryc ? a : b, andswitchwithbreak.??gives defaults for null/undefined;?.reads nested values safely.