The bike-share crew needs its Python dock report in JavaScript. Start with Harbor Square’s 14 bikes and 1 free slot, plus a counter for docks checked. You’ll keep the values and arithmetic, but choose a declaration for each name so readers can tell which names change.
Declare Each Name Once
Python creates a name on assignment. JavaScript requires a declaration, which introduces the name before you use it. These are the report’s main syntax changes:
| Purpose | Python | JavaScript |
|---|---|---|
| Name a dock | name = "Harbor Square" |
const name = "Harbor Square"; |
| Start a counter | docks_checked = 0 |
let docksChecked = 0; |
| Increase it | docks_checked += 1 |
docksChecked += 1; |
| Comment | # Dock list |
// Dock list |
| Print values | print(name, bikes) |
console.log(name, bikes); |
With const and let, declare each name once in the same scope: here, the top level of your file. Redeclaring it there gives a SyntaxError. With "type": "module" in package.json, your JavaScript file runs in strict mode, so assigning to an undeclared name gives a ReferenceError instead of creating it.
Semicolons end statements. JavaScript can usually insert missing ones, but this course writes them out. // comments run to the line’s end.
For the plain strings and numbers used here, console.log() separates arguments with spaces, like Python’s print(), but has no sep= or end= options. In Node, format markers such as %s in the first string can change that behavior.
Strings accept single or double quotes, and numbers look familiar. One difference matters when calculating how many bikes the van needs to fill half of Elm Street’s 12 free slots: Python’s 12 / 2 produces 6.0. Here’s the same division in JavaScript:
console.log(12 / 2);
JavaScript prints:
6
JavaScript uses one ordinary number type for whole and fractional values, so there’s no .0 to show here. Numbers, types, null, and equality get fuller treatment later.
Choose const or let
Use const by default because it prevents reassignment. Unlike Python’s UPPER_SNAKE_CASE convention or typing.Final hint, this is enforced when the program runs. Try reassigning Harbor Square’s bike count in the node REPL, its interactive shell:
> const bikes = 14;
undefined
> bikes = 15;
Uncaught TypeError: Assignment to constant variable.
The REPL displays undefined because a declaration has no result. The declaration succeeded, and bikes still holds 14.
The TypeError signals that the new assignment failed. Use let when you need to reassign a name, such as the counter. After its declaration, reassign without repeating the keyword. A const declaration prevents reassignment of the name, but you can still change the contents of arrays and objects. You’ll learn about these data structures later.
An ordinary const declaration must include its initial value: const bikes; gives a SyntaxError. If the value will come later, use let bikes; to declare bikes with an initial value of undefined, meaning no value has been supplied yet.
In JavaScript, camelCase conventionally replaces Python’s snake_case: each word after the first starts with a capital. docks_checked would still work. Put the report’s declarations and counter together in ~/dock-report/dock-report.js, with { "type": "module" } in that folder’s package.json:
// Harbor Square, from this morning's dock list
const name = "Harbor Square";
const bikes = 14;
const slots = 1;
let docksChecked = 0;
console.log(name, bikes, slots);
docksChecked += 1;
console.log("Docks checked:", docksChecked);
From ~/dock-report, run node dock-report.js to print the dock and count:
Harbor Square 14 1
Docks checked: 1
docksChecked needs let, because only that name is reassigned. Keep const for the other three names.
In the next lesson, Building Strings With Template Literals, you’ll turn the report’s f-strings into JavaScript strings containing values and expressions, then format a dock’s fill rate as a whole percentage.