Get Help And Discuss STEM Concepts From Math To Data Science & Financial Literacy
STEM Gender Equality | Join us on ZOOM | Spreading Mathematical Happiness | Join Us on Slack

MathsGee is free of annoying ads. We want to keep it like this. You can help with your DONATION

0 like 0 dislike
33 views

What's the difference between a variable that is: nullundefined or undeclared?

How would you go about checking for any of these states?

in Computer Science by Diamond (51,000 points) | 33 views

1 Answer

0 like 0 dislike

From what I understand, in Javascript, undefined and null are somewhat related on what value a variable contains. The case for undeclared differs. It tackles on how a variable is defined and how javascript treats these variables. So I am going to discuss undefined and null first since both are on Data Type category.

undefined is a variable that has been declared but no value exists and is a type of itself ‘undefined’.
null is a value of a variable and is a type of object.

We use ‘console.log();’ and ‘type of’ to check if a variable is undefined or null.

ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

undeclared variables is a variable that has been declared without ‘var’ keyword.
testVar = ‘hello world’;
as opposed to
var testVar = ‘hello world’;
When former code is executed, undeclared variables are created as global variable and they are configurable (ex. can be deleted).

ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

by Diamond (51,000 points)

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
0 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
0 answers

Welcome to MathsGee Q&A Bank, Africa’s largest personalized STEM and Financial Literacy education network that helps people find answers to problems, connect with others and take action to improve their outcomes.


MathsGee Q&A is the STEM and Financial Literacy knowledge-sharing community where students and experts put their heads together to crack their toughest questions.


MathsGee is free of annoying ads. We want to keep it like this. You can help with your DONATION

Enter your email address:

11,819 questions
9,839 answers
100 comments
10,516 users