Assigning by Mistake in a Condition
In this lesson, you saw how one stray = in the thank-you check made the release note credit Maya
on Theo’s day, and what an assignment produces in JavaScript.
- In JavaScript, an assignment is an expression: it produces the value it assigns.
- So a line like
deployer = nextDeployer = "Maya"works in JavaScript as it does in Python. JavaScript groups it from the right:nextDeployer = "Maya"runs first and produces"Maya", anddeployertakes that value. - In Python, the plain
=is a statement, and only the walrus:=produces a value. JavaScript needs no walrus. - A single
=in a JavaScript condition assigns, and theiftests the value it produced. Where Python stops with a SyntaxError, JavaScript runs. - In a condition, compare with
===. Python’s parser stops the slip for you; in JavaScript, a linter, a checker much like Ruff, is what flags it, before the file runs.
The script starts both deployers at Maya, the agency’s lead. Here’s the port’s line, after the two names are declared:
let deployer;
let nextDeployer;
deployer = nextDeployer = "Maya";
console.log("Next:", deployer, "then", nextDeployer);