

Falsy values in Javascript - Programmer and Software Interview Questions and Ans...
source link: https://www.programmerinterview.com/javascript/falsy-in-javascript/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

What is the meaning of “falsy” in Javascript?
As you probably already know, Javascript has the Boolean values true and false. But, what’s interesting and important is that you understand the fact that everything in Javascript has a built-in Boolean value in addition to it’s ‘obvious’ value, and that Boolean value is known as either falsy or truthy. This may sound confusing, but read on and the concept should become clearer.
Falsy values in Javascript
Here’s a complete list of falsy values in Javascript:
false (the boolean false is also considered falsy) "" (an empty string) 0 (zero) null undefined NaN (a property that represents the "Not-a-Number" value - indicating that a value is not a legal number)
The list above is the complete list of falsy values in Javascript.
Truthy values in Javascript
Now you’re probably wondering about truthy values. Well, every value that’s not in the list above is a truthy value. Even if we take false and put it in quotes – like this: “false” – then that is still a truthy value.
Why falsy values can be dangerous
Suppose we have the following code in an if statement:
if(someString.indexOf('findthis')) ...//do something
The code above checks to see if the ‘findthis’ text is somewhere inside of someString, and if it is it returns the index, which is the position inside someString where the substring ‘findthis’ starts. The problem with this code is the fact that the someString string could very well start with ‘findthis’ (the ‘someString’ string could be something like ‘findthisxzyabc’), which would mean that the indexOf check would return a 0 (because 0 is the position for the very first character in the string). And because 0 is a ‘falsy’ value – meaning that it is treated as a boolean false inside the if statement – then the if statement would evaluate to false even though it should be true in this situation because of the fact that the substring ‘findthis’ was actually found inside someString.
So, you should be very careful when writing your if statements – especially when checking properties that have the potential to return a falsy value. In that situation you should be prepared to handle it appropriately.
Javascript falsy tests:
It’s worth taking some time to go over the different falsy values to test them out and see how they compare to each other.
False, 0, and “” – all falsy values – are considered to be equivalent and can be compared to one another. This is what that would look like:
0 == false //this is true "" == false //this is true 0 == "" //this is also true
However, the values undefined and null are considered to not be equivalent to anything except themselves. So, here is what we would have:
(null == false) // false (null == null) // true (undefined == undefined) // true (undefined == null)// true
And the strange thing about NaN is that it is not considered to be equivalent to anything and that includes itself:
NaN == undefined //false NaN == NaN //false
Recommend
-
2
Truthy and falsy values are two important concepts for JavaScript developers to understand. In this tutorial, you will learn about what truthy and falsy values in JavaScript are and how they work. You will also learn how to convert values to...
-
8
How would you find out if a string contains another string in PHP? Suppose we have a string that is stored in a PHP variable called $aString. And we want to find out if inside of $aString there is another substring – let’s just sa...
-
8
How do I find out the version of WordPress that I am currently running? You may be having a difficult time determining the current version of WordPress that your website is running – for newer versions it seems that the administrato...
-
9
How to find the number of downloads for an app in the itunes store? The number of downloads for a particular application in the iTunes store is not made public by Apple – and most app developers don’t make that information public ei...
-
11
In C++, what’s the difference between #include ” ” and #include<>? What is the purpose of #include in C++? The purpose of using the #include directive is so that a copy of the file that is named in the include direct...
-
11
What is a wrapper object in Javascript? Provide an example of how a wrapper object would work. In order to best explain what wrapper objects are and how they work in Javascript, let’s first consider a simple example of a string in...
-
7
Dealing with Consultants/Recruiters in India Having gone through the job search process in India, I’d like to share some of my observations on recruiters or consultants in India – people who specialize in placing IT candi...
-
10
Interviewing in India Since India clearly has such a large number of programmers, I wanted to devote a separate section with advice on how to approach programmer interviews in India. Of course, the technical que...
-
7
When is Javascript used outside of the browser? Javascript is commonly known by programmers as a language executed by browsers to validate forms, add extra functionality to webpages, etc. Because of the fact that Javascript...
-
9
When should you use the “var” keyword in Javascript? How does the “var” keyword work? The answer to the question is easy: you should always use the “var” keyword in Javascript. But, you should also understand why you should always...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK