this will do the trick for you

if (!!val) {
    alert("this is not null")
} else {
    alert("this is null")
}
🌐
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
To check that a value is not an empty string, null, or undefined, you can create a custom function that returns true if a value is null, undefined, or an empty string and false for all other falsy values and truthy values:
Discussions

Good way to check for variable being not null and not undefined.
There are some style guides that basically say you always should use ===, but you can use == in order to check for null or undefined at the same time. So you could do the following: if (value != null) { // This will run if `value` is not `null` and not `undefined`. } More on reddit.com
🌐 r/javascript
56
32
October 20, 2016
What’s the best way to use JavaScript if not null to check if a variable is not null? - LambdaTest Community
What’s the best way to use JavaScript if not null to check if a variable is not null · This solution is the most general and checks if myVar is a truthy value. It works for any value that is not falsy, including null, undefined, false, 0, NaN, and "". It’s useful if you want to ensure ... More on community.lambdatest.com
🌐 community.lambdatest.com
0
March 31, 2025
Good way to check for variable being not null and not undefined. : javascript
Hi, all, I often do stuff like this in my code, to check for a variable being not null and not undefined. // check if value is not null and... More on old.reddit.com
🌐 r/javascript
What is the best method to check if a variable is not null or empty?
It depends what you mean by empty, or how strict you want to be. These values will coerce to false: undefined null '' (empty string) 0 NaN Everything else coerces to true. So, if you are OK with rejecting all of those values, you can do: if(PostCodeInformation) { } If you want to make sure that PostCodeInformation is really an object value (and not a number or boolean, etc): if(typeof PostCodeInformation === 'object' && PostCodeInformation !== null) { } You have to do the null-check there, because in JavaScript typeof null returns 'object'. So dumb. If you want to make sure that PostCodeInformation has some property that you really need: if(PostCodeInformation && PostCodeInformation.myCoolProperty) { } Etc, etc More on reddit.com
🌐 r/javascript
18
3
August 2, 2015
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Optional_chaining
Optional chaining (?.) - JavaScript | MDN
July 8, 2025 - For example, if obj.first is a Falsy value that's not null or undefined, such as 0, it would still short-circuit and make nestedProp become 0, which may not be desirable. With the optional chaining operator (?.), however, you don't have to explicitly test and short-circuit based on the state of obj.first before trying to access obj.first.second: ... By using the ?. operator instead of just ., JavaScript knows to implicitly check to be sure obj.first is not null or undefined before attempting to access obj.first.second.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-for-null-values-in-javascript
How to check for null values in JavaScript ? - GeeksforGeeks
July 23, 2025 - Checking undeclared variable: true false true Checking declared but undefined variable: declaredButUndefinedVariable is neither null nor undefined: false Checking string variable: stringVariable is neither null nor undefined: true Additional test cases: null is neither null nor undefined: false undefined is neither null nor undefined: false 0 is neither null nor undefined: true false is neither null nor undefined: true
🌐
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
August 4, 2022 - 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.
🌐
Favtutor
favtutor.com › articles › null-javascript
Check for Null in JavaScript | 3 Easy Methods (with code)
January 5, 2024 - In this section, we will look into various methods to check for a null value. We can use the strict equality operator (===) in JavaScript to check if a variable is explicitly set to null.
Find elsewhere
🌐
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 - In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing
Nullish coalescing operator (??) - JavaScript | MDN
Combining them, you can safely access a property of an object which may be nullish and provide a default value if it is. ... const foo = { someFooProp: "hi" }; console.log(foo.someFooProp?.toUpperCase() ?? "not available"); // "HI" console.log(foo.someBarProp?.toUpperCase() ??
🌐
Omarileon
omarileon.me › blog › typescript-null-undefined
mari. | How to Detect Null and Undefined in Your TypeScript Code
February 27, 2024 - So for the strict equality operator, "null !== undefined", but the regular equality operator will consider them the same.
🌐
ThatSoftwareDude.com
thatsoftwaredude.com › content › 8774 › what-is-the-best-way-to-check-for-an-empty-string-in-javascript
The Best Way to Check for an Empty String in JavaScript - ThatSoftwareDude.com
August 23, 2024 - Discover the most efficient methods to check for an empty string in JavaScript. Learn best practices to ensure your code handles string validation effectively.
🌐
DEV Community
dev.to › wolfhoundjesse › null-checking-in-javascript-lc4
Null-checking in JavaScript - DEV Community
April 11, 2019 - Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript! ... Looking at this block of code... if ( tokenInfo && tokenInfo !== undefined && tokenInfo !== null && tokenInfo !== "" ) { } If the first check... ... ... is undefined, null, or "", or any other falsy valuue... then the if will short circuit and not perform any of the other checks.
🌐
LambdaTest Community
community.lambdatest.com › general discussions
What’s the best way to use JavaScript if not null to check if a variable is not null? - LambdaTest Community
March 31, 2025 - What’s the best way to use JavaScript if not null to check if a variable is not null · This solution is the most general and checks if myVar is a truthy value. It works for any value that is not falsy, including null, undefined, false, 0, NaN, and "". It’s useful if you want to ensure ...
🌐
W3Schools
w3schools.com › sql › sql_null_values.asp
SQL NULL Values - IS NULL and IS NOT NULL
SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NOT NULL; Try it Yourself » ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected] · If ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript | MDN
October 28, 2025 - 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; ...
🌐
Programiz
programiz.com › javascript › examples › check-undefined-null
JavaScript Program To Check If A Variable Is undefined or null
To understand this example, you should have the knowledge of the following JavaScript programming topics: ... // program to check if a variable is undefined or null function checkVariable(variable) { if(variable == null) { console.log('The variable is undefined or null'); } else { console.log('The variable is neither undefined nor null'); } } let newVariable; checkVariable(5); checkVariable('hello'); checkVariable(null); checkVariable(newVariable);
🌐
Medium
medium.com › @python-javascript-php-html-css › how-to-check-for-empty-undefined-or-null-strings-in-javascript-d8f0bf514ead
How to Use JavaScript to Check for Null, Empty, or Undefined Strings
August 24, 2024 - Use value == null to check for both null and undefined. ... String validation ensures data integrity and prevents errors in your application. ... Ensuring that strings are properly validated in JavaScript is crucial for maintaining robust and ...
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-null-check
How to Check for Null in JavaScript | Built In
Comparisons can be made: null === null to check strictly for null or null == undefined to check loosely for either null or undefined. 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...
Published   August 4, 2025
🌐
Zipy
zipy.ai › blog › how-do-i-check-for-an-empty-undefined-null-string-in-javascript
how do i check for an empty undefined null string in javascript
April 12, 2024 - This approach takes advantage of the fact that an empty string (""), undefined, and null are all falsy values in JavaScript. Thus, the !myString condition checks for all three cases simultaneously.
🌐
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: