+1 vote
in JavaScript by
What's the output?
let greeting;
greetign = {}; // Typo!
console.log(greetign);
  • A: {}
  • B: ReferenceError: greetign is not defined
  • C: undefined

1 Answer

0 votes
by

Answer: A

It logs the object, because we just created an empty object on the global object! When we mistyped greeting as greetign, the JS interpreter actually saw this as:

  1. global.greetign = {} in Node.js
  2. window.greetign = {}frames.greetign = {} and self.greetign in browsers.
  3. self.greetign in web workers.
  4. globalThis.greetign in all environments.

In order to avoid this, we can use "use strict". This makes sure that you have declared a variable before setting it equal to anything.

...