JS Coding Questions Logo
JS Coding Questions
#81💼 Interview💻 Code

What are the differences between undeclared and undefined variables

Advertisement

728x90

Below are the major differences between undeclared(not defined) and undefined variables,

| undeclared | undefined |

| ------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |

| These variables do not exist in a program and are not declared | These variables declared in the program but have not assigned any value |

| If you try to read the value of an undeclared variable, then a runtime error is encountered | If you try to read the value of an undefined variable, an undefined value is returned. |

javascript
1var a;
2
3a; // yields undefined
4
5b; // Throws runtime error like "Uncaught ReferenceError: b is not defined"

This can be confusing, because it says not defined instead of not declared (Chrome)

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 82

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
81of476
What are the differences between undeclared and undefined variables | JSCodingQuestions.com