What are Differences between undefined and not defined in javascript?

Forums JavaScriptWhat are Differences between undefined and not defined in javascript?
Staff asked 2 years ago

Answers (2)

Add Answer
Ashish Kakadiya Marked As Accepted
Staff answered 2 years ago
  • error as undefined :

It works like when we declared a variable in the code but did not assign the value before printing the variable value.

Example:

let name;
console.log(name); //undefined

 

  • error as not defined:

It works like when we did not declare the variable and try to call that variable.

Example :

<script>
  console.log(a);
  var a = 5;
  console.log(a);
  console.log(b);
</script>
// refference error (b) is not defined.
Staff answered 2 years ago

If the variable name which is being accessed doesn’t exist in memory space then it would be not defined, and if exists in memory space but hasn’t been assigned any value till now, then it would be undefined.

Subscribe

Select Categories