Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Making Decisions With if and else

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 writes else if, an else followed by a second if, right after the closing brace. The last else gets braces and no colon.
  • JavaScript spells and, or and not with symbols, the logical operators &&, || and !. Carry the word or across, and JavaScript stops with a SyntaxError before any of the file runs.

Here’s the crew’s Python script for Station Road:

Language: Python
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}")

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:02 Before each shift, the crew’s Python script decides what each dock needs. For Harbor Square, with fourteen bikes and one free slot, it prints that the dock is nearly full.

00:15 A dock is empty at zero bikes, and nearly full at two free slots or fewer. Any other dock is fine. In this lesson, you’ll port that decision to JavaScript.

00:27 Your port already names the dock’s three values.

00:33 Start with the first test. In JavaScript, the condition goes in parentheses, and an opening brace takes the place of the colon. The comparison changes too.

00:45 JavaScript compares with three equals signs. It has a two-sign version as well, and why you leave that one alone is a later topic. The print line ports as a template literal, and a closing brace ends the block.

01:01 Next, Python’s elif. JavaScript has no elif keyword. It writes else, then a second if, right after the closing brace.

01:12 That second if gets its own parentheses and braces. The less than or equal sign is the same in both languages. The last else gets braces and no colon, and one more brace closes the chain.

01:28 Run the port, and it prints that Harbor Square is nearly full, as Python did. That first test, with three equals signs, is called strict equality.

01:42 Why all the punctuation? JavaScript doesn’t read indentation. The braces tell it where each block starts and stops, and the indentation inside them is only for you.

01:54 Scramble the indentation inside the braces and run it again. It still prints that Harbor Square is nearly full. So always write the braces. The full story comes later.

02:09 Harbor Square also needs the van. The van stops at any dock that’s empty or nearly full, and Python joins the two tests with or. JavaScript has no or keyword.

02:23 It spells it with two vertical bars, and the rest ports as before. Run it, and the van stops at Harbor Square. Watch out: carry the word or across, and JavaScript stops with a syntax error before any of the file runs.

02:42 The van can skip a dock with bikes and room. Python joins those tests with and. JavaScript spells and with two ampersands. Try it on the crew’s next dock, Station Road, with six bikes and eight free slots.

03:00 Run it, and it prints that Station Road is fine, and that the van can skip it. Neither of the van’s stop tests holds, so that line prints nothing.

03:12 Python’s last word, not, is an exclamation mark in JavaScript, written right before the test it flips.

03:21 Run this for Station Road, and both print that it has room. These three symbols are JavaScript’s logical operators.

03:31 So here’s the rule. In JavaScript, a condition goes in parentheses and its block in braces, with else if where Python writes elif.

03:42 You compare with strict equality, and you join and flip tests with the logical operators.

03:48 Next up is the text lesson Looping Over an Array With for…of, where you’ll go through all five docks’ bike counts with JavaScript’s loop over a list. Then you’ll count how many docks are empty, with a let counter and an if inside the loop.

Become a Member to join the conversation.