Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

JS
JavaScript
0/12 completed
Beginner
What is JavaScript?Variables and data typesOperators and expressionsMaking decisions: if and switchLoops
Intermediate
FunctionsArrays and array methodsObjects
Advanced
Scope and closuresClasses and OOPPromises and async/await
Pro
The event loop
Tracks / JavaScript / Beginner

Making decisions: if and switch

Programs need to make choices: show a login button or a profile picture, accept a payment or reject it. Conditionals run different code depending on whether something is true.

if, else if, else

JavaScript · editable

JavaScript checks each condition in order and runs only the first block that matches. Change temperature and run it again.

Combining conditions

JavaScript · editable

The ternary operator

For a simple "this or that" choice, the ternary operator condition ? a : b fits on one line.

JavaScript · editable

switch

When you compare one value against many options, switch can be cleaner than a long if/else chain.

JavaScript · editable

Don't forget break. Without it, JavaScript "falls through" and runs the next case too. Sometimes that's useful (see 'sat' and 'sun' above), but usually it's a bug.

Guard clauses

Pros often check for bad cases first and exit early. This keeps code flat and readable.

JavaScript · editable

(Functions are covered properly in a later lesson. For now, notice how each return exits immediately.)

Practice: grade calculator

Print a grade from a marks value: 90+ → A, 75+ → B, 50+ → C, otherwise → F. Also print "Invalid" if marks are below 0 or above 100.

JavaScript · editable
✎ Check your understanding

1.With x = 15, what does this print? if (x > 10) log('A') else if (x > 5) log('B')

2.What happens if you forget break in a switch case?

← Operators and expressions