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

Frontend Masters logo

Frontend Masters is the best place on the web to really learn JavaScript. We have a complete learning path from the biggest and best teachers in JavaScript to help you make the most out of the web's biggest language.

7-Day Free Trial

Leave a Reply

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

$839,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.