They are not equivalent. The first will execute the block following the if statement if myVar is truthy (i.e. evaluates to true in a conditional), while the second will execute the block if myVar is any value other than null.

The only values that are not truthy in JavaScript are the following (a.k.a. falsy values):

  • null
  • undefined
  • 0
  • "" (the empty string)
  • false
  • NaN
Answer from Tim Down on Stack Overflow
People also ask

Is null false in JavaScript?
Null is not considered false in JavaScript, but it is considered falsy. This means that null is treated as if itโ€™s false when viewed through boolean logic. However, this is not the same thing as saying null is false or untrue.
๐ŸŒ
builtin.com
builtin.com โ€บ software-engineering-perspectives โ€บ javascript-null-check
How to Check for Null in JavaScript | Built In
What is a null check?
In JavaScript, null represents an intentional absence of a value, indicating that a variable has been declared with a null value on purpose. On the other hand, undefined represents the absence of any object value that is unintentional. A null check determines whether a variable has a null value, meaning a valid instance of a type exists.
๐ŸŒ
builtin.com
builtin.com โ€บ software-engineering-perspectives โ€บ javascript-null-check
How to Check for Null in JavaScript | Built In
What is a strict null check?
StrictNullChecks is a feature that treats null and undefined as two separate types, reducing errors and making it easier to find coding bugs. It also has stronger measures for defining variables as null or undefined, ensuring variables are declared as null only when itโ€™s safe to do so.
๐ŸŒ
builtin.com
builtin.com โ€บ software-engineering-perspectives โ€บ javascript-null-check
How to Check for Null in JavaScript | Built In
๐ŸŒ
Built In
builtin.com โ€บ software-engineering-perspectives โ€บ javascript-null-check
How to Check for Null in JavaScript | Built In
The value null is falsy, but empty objects are truthy, so typeof maybeNull === "object" && !maybeNull is an easy way to check to see that a value is not null. Finally, to check if a value has been declared and assigned a value that is neither ...
Published ย  August 4, 2025
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-check-if-a-variable-is-not-null-in-javascript
How to check if a Variable Is Not Null in JavaScript ? - GeeksforGeeks
August 5, 2025 - Example: In this example we are using typeof operator with !== undefined and !== null checks if a variable is not null in JavaScript.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Operators โ€บ null
null - JavaScript | MDN
If you are designing an API, you should likely accept null and undefined as equivalent inputs, because many codebases have stylistic rules about when to use null or undefined by default. When checking for null or undefined, beware of the differences between equality (==) and identity (===) operators, as the former performs type-conversion. ... typeof null; // "object" (not "null" for legacy reasons) typeof undefined; // "undefined" null === undefined; // false null == undefined; // true null === null; // true null == null; // true !null; // true Number.isNaN(1 + null); // false Number.isNaN(1 + undefined); // true
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ javascript
How to check if value is undefined or null in JavaScript
June 8, 2023 - If you want to check if a value is specifically undefined or null without type coercion, you can use the identity operator (===). The identity operator checks for both value and type equality, which means it does not perform type coercion.
Find elsewhere
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-check-for-null-in-javascript
JS Check for Null โ€“ Null Checking in JavaScript Explained
November 7, 2024 - But, this can be tricky because if the variable is undefined, it will also return true because both null and undefined are loosely equal. let firstName = null; let lastName; console.log(firstName == null); // true console.log(lastName == null); // true console.log(firstName == undefined); // true console.log(lastName == undefined); // true console.log(firstName == lastName); // true console.log(null == undefined); // true ยท Note: This can be useful when you want to check if a variable has no value because when a variable has no value, it can either be null or undefined.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ javascript-check-if-variable-is-a-undefined-or-null
JavaScript: Check if Variable is undefined or null
March 29, 2023 - In this short guide, we've taken a look at how to check if a variable is null, undefined or nil in JavaScript, using the ==, === and typeof operators, noting the pros and cons of each approach.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ how do i check for an empty/undefined/null string in javascript?
How do I Check for an Empty/Undefined/Null String in JavaScript? | Sentry
The isEmpty function uses the equality operator (==) to check if the argument value is null or undefined. This works because if one of the operands is null or undefined, the other operand must be null or undefined for the equality comparison ...
๐ŸŒ
DEV Community
dev.to โ€บ wolfhoundjesse โ€บ null-checking-in-javascript-lc4
Null-checking in JavaScript - DEV Community
April 11, 2019 - What are your thoughts? How are you checking for empty or null values? Bonus if you comment with examples in other programming languages! ... It's more a bright exemple of how to make simple things looks complicated because in javascript if(tokenInfo) will check if variable is either :
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-check-if-variable-is-not-null
Check if a Variable is Not NULL in JavaScript | bobbyhadz
Copied!const a = 'hello'; if (a) { console.log(`๐Ÿšจ a is NOT false, 0, empty string, null, undefined, NaN`); } else { console.log(`โ›”๏ธ๏ธ a is ONE OF false, 0, empty string, null, undefined, NaN`); } In this example, we check if the value stored in the variable a is truthy. Truthy are all values that are not falsy. The falsy values in JavaScript are: false, 0, "", null, undefined, NaN.
๐ŸŒ
LogRocket
blog.logrocket.com โ€บ home โ€บ how to check for null, undefined, or empty values in javascript
How to check for null, undefined, or empty values in JavaScript - LogRocket Blog
February 14, 2025 - Learn how to write a null check function in JavaScript and explore the differences between the null and undefined attributes.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ check-if-string-is-empty-or-null-javascript
How to Check if a String is Empty or Null in JavaScript โ€“ JS Tutorial
November 7, 2024 - In this example, we're first using the trim method to remove any leading or trailing whitespace characters from the str variable, then checking whether the resulting string has zero length. If it does, then we know that the string is empty. Otherwise, we know that the string is not empty. Here are some best practices to follow when checking for empty or null strings in JavaScript:
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-do-i-check-for-null-values-in-javascript
How do I check for null values in JavaScript?
The typeof operator may be used to determine the data type of a JavaScript variable. Here we use the typeof operator with the null operator. The (!variable) means the value is not null and if it is checked with the typeof operator variable which has the data type of an object then the value is null.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-check-for-null-values-in-javascript
How to check for null values in JavaScript ? - GeeksforGeeks
July 23, 2025 - Lodash _.isNull() method is used to find whether the value of the object is null. If the value is null then returns true otherwise it returns false. ... Example: In this example, we are checking whether the given value is null or not by the ...
๐ŸŒ
Futurestud.io
futurestud.io โ€บ tutorials โ€บ check-if-a-value-is-null-or-undefined-in-javascript-or-node-js
Check if a Value Is Null or Undefined in JavaScript or Node.js
Iโ€™m the maintainer of the @supercharge/goodies package providing convenient helper functions for Node.js and JavaScript. This package comes with a handy isNullish(value) method determining whether the given value is null or undefined: const { isNullish } = require('@supercharge/goodies') isNullish(null) // true isNullish(undefined) // true isNullish(0) // false isNullish('') // false isNullish({}) // false isNullish([]) // false isNullish(false) // false ยท Notice: the isNullish method is fully typed and your code editor or IDE knows that the provided value is either null/undefined or not.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-check-if-a-variable-is-not-null-in-javascript
How to check if a variable is not NULL in JavaScript - javatpoint
How to check if a variable is not NULL in JavaScript with javascript tutorial, introduction, javascript oops, application of javascript, loop, variable, objects, map, typedarray etc.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-determine-if-variable-is-undefined-or-null-in-javascript.php
How to Determine If Variable is Undefined or NULL in JavaScript
In simple words you can say a null ... if a variable is undefined or null you can use the equality operator == or strict equality operator === (also called identity operator)....