JS Interview Question: What’s the difference between a variable that is: null, undefined or undeclared?

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

Also

If you are looking for a platform to develop your clients’ website, check out my SAAS platform https://ecompurpl.com :)

EcomPurpl.com is the all-in-one solution for anyone looking to create a website. Templates, E-Commerce, Hosting, Galleries, Booking & Appointments, CRM, 24/7 support, and integration of your app are all included.

--

--

--

Software Engineer / Front-End Dev. Currently resides in Las Vegas. https://ecompurpl.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Be More Functional with React.memo + TypeScript

Error handling in RxJava/RxKotlin

Scopes & Closures Re-Explained: Part 1

The Observer Pattern in JavaScript — the Key to a Reactive Behavior

React JS

React

Making a SideBar in React with Bootstrap

Gradually using TypeScript in Your React Project

JavaScript beats Python to become the most popular coding language of 2021

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
RLyn Ben

RLyn Ben

Software Engineer / Front-End Dev. Currently resides in Las Vegas. https://ecompurpl.com

More from Medium

Conditional (ternary) operator in Javascript

JavaScript reduce() Method

What is Javascript Engine?

Object Oriented Programming w/ JavaScript| “this”, “new” & constructors