Ready. Start with one instruction.
Loading the learning workspace…
FROM YOUR FIRST INSTRUCTION TO A WORKING APPLICATION
Learn JavaScript from scratch through small working examples, visual experiments and complete projects. No account or AI key required.
Predict → Experiment → Build → Test → Explain
Ready. Start with one instruction.
Loading the learning workspace…
if evaluates a condition and chooses a branch. else runs only when that condition is false. Braces group the instructions belonging to each path. Both branches exist in the source, but only one runs for a given input. Think of a decision as a question about data rather than a command that must always succeed. The next examples show passing, failing and boundary values.
const score = 5;
if (score >= 5) {
console.log("Pass");
} else {
console.log("Practice");
}Pass
A score of five enters the first branch because equality is part of the condition.
const score = 5;Declare score with const. The binding cannot be reassigned. The expression after = supplies its initial value; = is assignment, not an equality comparison.
if (score >= 5) {Choose a branch according to this condition. Only the selected path runs for this input; inspect the exact comparison and its boundary.
console.log("Pass");Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
} else {Choose a branch according to this condition. Only the selected path runs for this input; inspect the exact comparison and its boundary.
console.log("Practice");Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
}Close the current instruction block.
const score = 3;
if (score >= 5) {
console.log("Pass");
} else {
console.log("Practice");
}Practice
A score of five enters the first branch because equality is part of the condition.
const score = 3;This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
if (score >= 5) {Choose a branch according to this condition. Only the selected path runs for this input; inspect the exact comparison and its boundary.
console.log("Pass");Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
} else {Choose a branch according to this condition. Only the selected path runs for this input; inspect the exact comparison and its boundary.
console.log("Practice");Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
}Close the current instruction block.
const score = 9;
if (score >= 5) {
console.log("Pass");
} else {
console.log("Practice");
}Pass
A score of five enters the first branch because equality is part of the condition.
const score = 9;This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
if (score >= 5) {Choose a branch according to this condition. Only the selected path runs for this input; inspect the exact comparison and its boundary.
console.log("Pass");Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
} else {Choose a branch according to this condition. Only the selected path runs for this input; inspect the exact comparison and its boundary.
console.log("Practice");Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
}Close the current instruction block.
Repair the intentionally changed operation so the example produces the exact expected result. Then try the extension without copying the answer.
const score = 5;
if (score > 5) {
console.log("Pass");
} else {
console.log("Practice");
}const score = 5;
if (score >= 5) {
console.log("Pass");
} else {
console.log("Practice");
}Put a useful instruction in each path and check both outcomes.
Which operation changed the result, why did it work, and what would you test next?
Use a different value that could expose a boundary or mistake. Predict the result first, run it, and explain how the concept would help in a small real application.
Related: Previous lesson Next lesson
100 lessons across 20 modules, covering variables, operators, strings, conditions, loops, functions, scope, arrays, objects, DOM, events, state, testing, asynchronous JavaScript and application development.