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

Ending Statements With Semicolons

Ending Statements With Semicolons

In this lesson, you found out what ends a statement in JavaScript, and why the release note’s handover stopped when it was ported without semicolons.

  • In JavaScript, a semicolon ends a statement, not a newline.
  • With no semicolon, JavaScript usually fills one in when the next line can’t carry on the statement, as with the let after "Maya" here. That’s automatic semicolon insertion.
  • So a newline isn’t always the end. After a value, a line that starts with a square bracket can carry on, and the two lines join into one statement.
  • JavaScript with no semicolons is a real style that relies on the insertion rule. This course writes them, which keeps this swap separate from the line above. A line break straight after return still ends the statement, whatever semicolons you write, as Splitting Long Lines shows. A formatter, a layout tool a bit like Black in Python, then keeps the semicolons in place.

Here’s the Python handover. It swaps today’s and tomorrow’s deployer in one line:

Language: Python
deployer = "Maya"
next_deployer = "Theo"
deployer, next_deployer = next_deployer, deployer
print("Next:", deployer, "then", next_deployer)

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 The release note ends with a handover. Python swaps today’s and tomorrow’s deployer in one line, and the note says Theo, then Maya. In this lesson, you’ll find out what marks the end of a statement in JavaScript.

00:19 In the JavaScript port, both are lets, because the swap reassigns them. The swap becomes square brackets on both sides, a kind of unpacking called destructuring that gets a proper look later on.

00:33 For now, it trades the two names. Like the Python, the port has no semicolons.

00:43 Does JavaScript print the same line as Python? Your turn: pause the video and predict.

00:54 No. It prints nothing and stops with a ReferenceError: it can’t access nextDeployer before initialization. The caret points into the swap line, at a name declared on the line above.

01:10 In JavaScript, a semicolon ends a statement, not a newline. With none after Maya, JavaScript checks the next line. Maya followed by let can’t be one statement, so JavaScript fills in the semicolon.

01:27 That’s automatic semicolon insertion. It only steps in when the next line can’t carry on. So a newline isn’t always the end. After Theo, a line that starts with a square bracket can carry on, so no semicolon goes in, where the swap needed one.

01:47 The two lines join into one statement. The bracket indexes into the string Theo, like s[0] in Python, using nextDeployer, the name this line is still declaring.

01:59 It has no value yet, so JavaScript refuses to read it.

02:05 So write the semicolons yourself, one at the end of every statement. Run it again: Theo, then Maya, the same as Python.

02:16 Semicolon-free JavaScript is a real style that relies on the insertion rule. This course writes them, so the rule never bites. A formatter, a layout tool a bit like Black in Python, then keeps them in place.

02:31 Next up is Splitting Long Lines, a text lesson, where the long code freeze check breaks with no backslash.

Become a Member to join the conversation.