How to Check for an Object in Javascript (Object Null Check)

How to Check for an Object in Javascript (Object Null Check)

When you check for an object in JavaScript, you also need to include a null check, because the typeof null is "object".

Object / Null Check in JavaScript

There’s a long-standing bug in JavaScript meaning that every check for an object requires a check for nulltypeof null === "object".

Typically, you’ll check for null using the triple equality operator (=== or !==), also known as the strict equality operator, to be sure that the value in question is definitely not null: object !== null. That code checks that the variable object does not have the value null.

Some developers prefer the double equality operator (== or !=), also called the loose equality operator. Writing object != null would check for undefined and null at the same time.

The typeof undefined is what you would expect (the string "undefined"), so typeof object === "object" will guarantee that you either have an object or a null value; you can’t have an undefined value.

And whether you check object !== null or object != null ends up being your personal preference when type checking JavaScript objects.

Many developers always use the === triple equality operator to avoid any confusion in their JavaScript code, since the == loose equality operator will perform type coercion and has its own rules.

To be explicit that you’re checking for both null and undefined while checking for an object, you have to specify both checks with the && (logical AND) operator: object !== null && object !== undefined.

Really, though, you only need to check for null check for a JavaScript object. It’s the ol’ JavaScript Object Null Check™ (since 1995).

programming web-development javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Top Web Development Company in India

We are the best offshore web development company in India providing web applications on Blockchain, AI & Chatbots. 15+ Yrs Exp., Save upto 60% on development.

Hire Best JavaScript Developers | JavaScript development company USA

Hire Dedicated JavaScript Developers from a top javascript development agency for your business solutions. We provide JS web developers for hire at a fixed or hourly rate.

What Garbage Collection in JavaScript Is and How It Works

JavaScript values are allocated when things are created (objects, Strings, etc.) and freed automatically when they are no longer used. This process is called Garbage collection.