-4

Case 1 - If I console.log(variable) before the variable declaration I get undefined. eg;

  // code 
  console.log(a);
  var a ;

  // output
  undefined

Case 2 - If I console.log(variable) without variable declaration I get Uncaught ReferenceError: variable is not defined.

// code
console.log(a);

// output
Uncaught ReferenceError: a is not defined

But incase of functions we can call a function before or after function definition it never give any issue. eg;

  console.log(example());

  function example(){
    return 'test done';
  }
  console.log(example());

 // output without any issue

Now My question is, what is the difference between undefined and not defined.

Achyut Kr Deka
  • 739
  • 1
  • 8
  • 18
  • Please do research more and google around it to get the answer automatically :) – Jitesh Sojitra Aug 18 '16 at 19:09
  • 1
    `var` and `function` declarations are hoisted. Welcome to JS. – zzzzBov Aug 18 '16 at 19:09
  • https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/var search for "hoisting" – Marc B Aug 18 '16 at 19:09
  • http://stackoverflow.com/q/500431/497418 would be good to read as well. This particular behavior is hard to search for when you don't understand what's going on. Don't feel too bad about the downvotes. – zzzzBov Aug 18 '16 at 19:10
  • This is because of the way the JS engine compiles functions VS variable. Undefined is a type in Javascript that is a variable has not been initialized to a value yet. Functions are hoisted so that you can call them from anywhere in your code (scope permitting). – Isabel Inc Aug 18 '16 at 19:12

1 Answers1

5

Undefined means - variable exists, but have no any stored value in it. Not defined means - variable not declared (not exist).

Celestine
  • 448
  • 7
  • 18