Falsey values vs null, undefned, or empty string

Posted by user687554 on Programmers See other posts from Programmers or by user687554
Published on 2014-08-18T22:03:29Z Indexed on 2014/08/18 22:31 UTC
Read the original article Hit count: 142

Filed under:

I've worked with jQuery over the years. However, recently, I've found myself getting deeper into the JavaScript language. Recently, I've heard about "truthy" and falsey values. However, I don't fully understand them. Currently, I have some code that looks like this:

var fields = options.fields || ['id', 'query'];

I need to identify if fields is null, undefined, or has a length of 0. I know the long way is to do:

if ((fields === null) || (fields === undefined) || (fields.length === 0)) {
 ...                    
}

My question is, is the following the same:

if (!fields)  {
 ...
}

Thank you!

© Programmers or respective owner

Related posts about JavaScript