0

I don't understand why I get an undefined result here.

(function() {
  'use strict';
  if (true) {
    function ok() {
      return 'true ok';
    }
  } else {
    function ok() {
      return 'false ok';
    }
  }
  console.log(typeof ok === 'undefined'); // => true
  console.log(ok()); // Throws "ReferenceError: ok is not defined"
})()

Why is this? Thanks

  • 1
    You are defining `ok()` functions that are only available in the `if` or `else` blocks. That is one of the points of `strict mode` – Seblor Jul 12 '17 at 15:19
  • Declarations are only available in the block in which they occur. (That it "works" in sloppy mode is only because of really weird back-compat semantics) – Bergi Jul 12 '17 at 15:26
  • if i declared var ok; immediately after "use strict", it works perfectly. simple! –  Jul 12 '17 at 15:39

0 Answers0