1

In this situation:

(function() {
   const a = b = 10;
})();
console.log('a:', a); // ReferenceError: a is not defined
console.log('b:', b); // 10

The b gets defined as a var in the global scope. Is it possible o do this equals trick but make b a const as well in the same scope? Just like a?

Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • please fix your snippet. the snippet you provided throws error `ReferenceError: b is not defined` and even if it didn't (perhaps in a different JS engine?) you still can't access `const` identifiers outside of their lexical scope – zhirzh Jan 16 '19 at 12:04

2 Answers2

1

You could set b as a parameter to your IIFE function. That way b won't be accessible outside of function scope.

(function(b) {
   const a = b = 10;
   console.log(b)
})();


console.log('b:', b);
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
1

I don't think so. This "trick" is possible because b = 10 returns 10. However, const b = 10 returns undefined.

Why not just use:

const a = 10;
const b = a;

?

It's not slower, and it's more readable, even if you could nest the assignments I don't really see a reason to do it

dquijada
  • 1,697
  • 3
  • 14
  • 19
  • Thanks I was studying for interview questions and they didn't ask how to fix, they just expected us to know that `b` got globalized, but i was curious how to fix. – Noitidart Jan 16 '19 at 13:36