Why JavaScript variables don’t always update

Chris Coyier Chris Coyier on

I liked this bit of JavaScript trivia from Cassidy Williams.

Look at this JavaScript code:

let [CLOSED, OPEN] = [{}, {}];
CLOSED = {
  cake: OPEN,
};
OPEN = {
  fish: 5,
};
console.log(CLOSED); // cake is {}Code language: JavaScript (javascript)

Why is it that CLOSED is not { cake: { fish: 5 }}?

There is a clear answer, and it’s something worth making sure you understand before heading into a job interview that involves JavaScript.

It's time to take your JavaScript to the next level

Leave a Reply

Your email address will not be published. Required fields are marked *

$966,000

Frontend Masters donates to open source projects through thanks.dev and Open Collective, as well as donates to non-profits like The Last Mile, Annie Canons, and Vets Who Code.