Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / How do I check for null values in JavaScript?

How do I check for null values in JavaScript?

August 1, 2021 by James Palmer

Javascript is very flexible with regards to checking for “null” values. I’m guessing you’re actually looking for empty strings, in which case this simpler code will work:
if(!pass || !cpass || !email || !cemail || !user){

Which will check for empty strings (“”), null, undefined, false and the numbers 0 and NaN
Please note that if you are specifically checking for numbers it is a common mistake to miss 0 with this method, and num !== 0 is preferred (or num !== -1 or ~num (hacky code that also checks against -1)) for functions that return -1, e.g. indexOf)

To check for null SPECIFICALLY you would use this:
if (variable === null)

This test will ONLY pass for null and will not pass for “”, undefined, false, 0, or NaN.
Additionally, I’ve provided absolute checks for each “false-like” value (one that would return true for !variable).
Note, for some of the absolute checks, you will need to implement use of the absolutely equals: === and typeof.
I’ve created a JSFiddle here to show all of the individual tests working
Here is the output of each check:
Null Test:

if (variable === null)

– variable = “”; (false) typeof variable = string

– variable = null; (true) typeof variable = object

– variable = undefined; (false) typeof variable = undefined

– variable = false; (false) typeof variable = boolean

– variable = 0; (false) typeof variable = number

– variable = NaN; (false) typeof variable = number

Empty String Test:

if (variable === ”)

– variable = ”; (true) typeof variable = string

– variable = null; (false) typeof variable = object

– variable = undefined; (false) typeof variable = undefined

– variable = false; (false) typeof variable = boolean

– variable = 0; (false) typeof variable = number

– variable = NaN; (false) typeof variable = number

Undefined Test:

if (typeof variable == “undefined”)

— or —

if (variable === undefined)

– variable = ”; (false) typeof variable = string

– variable = null; (false) typeof variable = object

– variable = undefined; (true) typeof variable = undefined

– variable = false; (false) typeof variable = boolean

– variable = 0; (false) typeof variable = number

– variable = NaN; (false) typeof variable = number

False Test:

if (variable === false)

– variable = ”; (false) typeof variable = string

– variable = null; (false) typeof variable = object

– variable = undefined; (false) typeof variable = undefined

– variable = false; (true) typeof variable = boolean

– variable = 0; (false) typeof variable = number

– variable = NaN; (false) typeof variable = number

Zero Test:

if (variable === 0)

– variable = ”; (false) typeof variable = string

– variable = null; (false) typeof variable = object

– variable = undefined; (false) typeof variable = undefined

– variable = false; (false) typeof variable = boolean

– variable = 0; (true) typeof variable = number

– variable = NaN; (false) typeof variable = number

NaN Test:

if (typeof variable == ‘number’ && !parseFloat(variable) && variable !== 0)

— or —

if (isNaN(variable))

– variable = ”; (false) typeof variable = string

– variable = null; (false) typeof variable = object

– variable = undefined; (false) typeof variable = undefined

– variable = false; (false) typeof variable = boolean

– variable = 0; (false) typeof variable = number

– variable = NaN; (true) typeof variable = number

As you can see, it’s a little more difficult to test against NaN;

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in