⚡0 XP🔥0

Operators and Conditions

Lesson 2 of 7 ⏱ 4 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs JavaScript

Arithmetic, comparison and logical operators, strict equality (===), truthy and falsy values, if / else if / else, the ternary operator, switch and the ?? and ?. operators.

Arithmetic operators#

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
22 12 85 3.4
2
1024
16
5 4 5 9
Tip

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
▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
true false
true false
true false
true
Common Mistake

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.

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
Please enter your name
true true false

if / else if / else#

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
Hot day - drink water.
हिंदी में समझें

if से program फैसला लेता है। ऊपर से नीचे हर condition check होती है, और जो पहली सही (true) निकलती है उसी का block चलता है - बाकी छोड़ दिए जाते हैं। कोई भी सही न हो तो else चलता है।

The ternary operator#

A short if/else that returns a value:

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
Result: Fail
You have 1 item in your cart

switch#

Good when one value is compared with many fixed options:

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
Weekend batch: 10 AM

Don't forget break - without it, the code "falls through" into the next case.

?? and ?. - safe defaults#

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
100
0
undefined
City not given

Summary#

  • Arithmetic: + - * / % **; shortcuts +=, ++.
  • Compare with === / !==; combine with &&, ||, !.
  • Falsy: false, 0, "", null, undefined, NaN.
  • if / else if / else, the ternary c ? a : b, and switch with break.
  • ?? gives defaults for null/undefined; ?. reads nested values safely.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Grade calculator

Read a percentage with prompt() and print the grade:

Percentage Grade
90 and above A+
75 - 89 A
60 - 74 B
40 - 59 C
below 40 Fail

For the test input 82, the output is Grade: A.

Test input:
82
Expected output:
Grade: A
2

Leap year

A year is a leap year if it is divisible by 4 and not by 100, or divisible by 400. Check each year in the array years and print 2024 is a leap year or 1900 is not a leap year.

Expected output:
2024 is a leap year
1900 is not a leap year
2000 is a leap year
2026 is not a leap year

Quick Quiz

Q1.

What is 5 == "5" and 5 === "5"?

Q2.

What is 17 % 5?

Q3.

Which of these values is truthy?

Q4.

What does const fee = discount ?? 0; do?

Q5.

What is printed? console.log(age >= 18 ? "Adult" : "Minor") with age = 16