DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Javascript

Determine if variable is ‘undefined’ or ‘null’

Share

In JavaScript, you can check whether a variable is undefined or null or blank or has a value defined in it.

In JavaScript, ‘undefined’ means a variable has been declared but has not been assigned a value yet, such as:

var a;
console.log(a); //shows undefined
console.log(typeof a); //shows undefined

‘null’ is an assignment value. This can be assigned to a variable as a representation of no value:

var a = null;
console.log(a); //shows null
console.log(typeof a); //shows object

So, from the preceding examples, this is clear that undefined and null are two distinct types i.e. undefined is a type itself where as null is an object.

null === undefined // false
null == undefined // true
null === null // true

and,

null = 'value' // ReferenceError
undefined = 'value' // 'value'

So, null and undefined are both are used to represent the absence of some value.

You can just check whether the variable has a true value or not.
That means:

if( value ) {
...
}

Read Also: Getting Started with Handlebars JS

This will evaluate to true if value is not:
– null
– undefined
– NaN
– empty string (“”)
– 0
– false

If you do not know whether a variable exists or not i.e. if it was declared you should check with the typeof operator.

For instance,

if( typeof value !== 'undefined' ) {
...
}

If you know that a variable is declared at least, then, you should directly check with the value also.

It seems the most complete answer would be:

if( typeof value === 'undefined' || variable === null ){
// Do stuff
}

Read Also: Date Object – JavaScript

In modern browsers you can safely compare the variable directly to undefined:

if (value === undefined) {...}

You can use void operator to get the value of undefined.
This will work even if the global window.undefined value has been over-written:

if (value === void(0)) {...}

But don’t use void(0) directly.

Use instead:

var undefined = void(0);
console.log(value === undefined);

Although null can do things undefined does, this is more or less related to objects rather than scalars.
Indeed, JavaScript considers null itself an object — typeof null returns “object”.

Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

Recent Posts

Do Keyword-Focused Domains Have Relevance for Voice Search Today?

When you select cheap domain names, it can have a significant impact on the success…

1 day ago

VPS Hosting for Resource-Intensive Applications: What You Need to Know

Shared hosting struggles to keep up the performance commitment. Therefore, low-cost VPS hosting is an…

1 day ago

How Dermatologists Treat Melasma Without Over-Bleaching the Skin

Does your melasma return every summer despite months of treatment? Melasma appears as brown or…

2 days ago

The “Natural Aging” Look: How to Embrace Graceful Aging With the Right Care

Fine lines, sagging skin, and uneven tone—these are common signs of aging that many people…

2 days ago

How to Restore Facial Definition Without Fillers or Threads

Can you reverse facial aging without needles or surgery? High-Intensity Focused Ultrasound (HIFU) treatment creates…

2 days ago

How to Choose the Right Wire Cable (Thailand)

When completing a wiring job, it’s important that you choose the right wire cable to…

3 days ago