You always do this in JavaScript to make sure your console statements don’t blow up on IE when the console is not open.
1 2 |
|
But you’ll just end up with no console on any of the browsers that support the console because this is what gets output as JavaScript:
1 2 3 4 5 6 7 |
|
What happened was that the variable console was defined in the local scope and that’s what you’re setting when you assign the value. And that’s what gets used for the rest of the definition. The fix is to make sure to attach it to the global object, whatever that is given the context.
1 2 3 4 |
|
Outputs:
1 2 3 4 5 6 7 8 9 10 11 |
|
Or you could just do:
console?.log "foo"
But the exercise illustrates how you often have to think about the JS output until you manage to internalize it.