Why does only the variable I put at the bottom get executed without console.log?

const a = [1, 2, 3];
const doubled = a.map((num) => {
  return num * 2;
});

const x = 2;

x;
doubled;

When I run the above program in console, only the variable at the bottom gets executed. So in the above code, I will only get doubled as the output ([2, 4, 6]) but there won’t be any output from x. But if I have put x below doubled, then the output will be x (2) but nothing from doubled.

Why does this happen and what’s going on behind the scenes here that I’m not getting?

(When I console.log both of them, then the behaviour is as expected and both get logged, but I’m curious about this odd behaviour without console.log)

Thanks!

EDIT: You can reproduce this by going to https://repl.it/languages/javascript and pasting the above code there.

64 thoughts on “Why does only the variable I put at the bottom get executed without console.log?”