Making Decisions With if and else
In this lesson, you ported the crew’s decision about one dock, empty, nearly full or fine, from
Python’s if, elif and else to JavaScript.
- The condition goes in parentheses, and the block goes in braces, where Python uses a colon and
indentation:
if (bikes === 0) {. - JavaScript doesn’t read indentation. The braces mark where a block starts and stops, and the
indentation inside them is only for you: scramble it, and the chain still prints
Harbor Square is nearly full. Always write the braces. The full story comes later. - JavaScript compares with
===, called strict equality. It has a two-sign version as well, and why you leave that one alone is a later topic. - There’s no
elif: JavaScript writeselse if, anelsefollowed by a secondif, right after the closing brace. The lastelsegets braces and no colon. - JavaScript spells
and,orandnotwith symbols, the logical operators&&,||and!. Carry the wordoracross, and JavaScript stops with aSyntaxErrorbefore any of the file runs.
Here’s the crew’s Python script for Station Road:
name = "Station Road"
bikes = 6
slots = 8
if bikes == 0:
print(f"{name} is empty")
elif slots <= 2:
print(f"{name} is nearly full")
else:
print(f"{name} is fine")
if bikes == 0 or slots <= 2:
print(f"The van stops at {name}")
if bikes > 0 and slots > 2:
print(f"The van can skip {name}")