Awarious OfficePortal - HRMS Application

Is there any easy way to check is null or empty in Javascript?

0 votes
asked Aug 10, 2014 in javascript library by Priya Roy (122 points)

Is there any standard function available in Javascript to check is null or empty of an variable instead of writing like below

if (val != "" && val != null val != undefined)
   x = val;



3 Answers

+1 vote
answered Aug 10, 2014 by Kannan Chandrasekara (112 points)
selected Aug 10, 2014 by Priya Roy
Best answer

The easiest way to check is

if (val){

but if you want to check just for empty string then go for.

if(val != ""){

Hope this helps.

0 votes
answered Aug 11, 2014 by administrator (315 points)

If you are using JQuery, then you can use

0 votes
answered Aug 8, 2016 by tamarasherwood (54 points)

You can just check if the variable has a truthy value or not. That means

if( value ) {

will evaluate to true if value is not:

empty string ("")

The above list represents all possible falsy values in ECMA-/Javascript. Find it in the specification at the ToBoolean section.

Furthermore, if you do not know whether a variable exists (that means, if it was declared) you should check with the typeof operator. For instance

if( typeof foo !== 'undefined' ) {
// foo could get resolved and it's defined

If you can be sure that a variable is declared at least, you should directly check if it has a truthy value like shown above.

Your answer


Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2019