11

I have a question concerning some concepts in JavaScript such as (truthy, true) and (falsy, false).

I know the type of 1 is not true but the question is: why 1 == true?

What was the main reason of ECMAScript to consider 1 or "ghsagh" as true?

I also cannot understand the meaning of truthy and falsy.

What was the benefit of this consideration?!

Dominik
  • 6,078
  • 8
  • 37
  • 61
Amir Jalilifard
  • 2,027
  • 5
  • 26
  • 38
  • 3
    possible duplicate of [Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons) – DarkDust Feb 11 '15 at 14:26
  • 1
    http://stackoverflow.com/a/359509/3083093 – R3tep Feb 11 '15 at 14:26
  • 2
    http://stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-when-tested-by-if-it-is-not-fals – epascarello Feb 11 '15 at 14:27
  • 2
    With these comments, I got sure that no one even read my question right! I know the difference between == and ===. The issue is the meaning of truthy and falsy – Amir Jalilifard Feb 11 '15 at 14:33
  • You have several questions in one. The linked questions and answers are to this "subquestion" of yours: _the question is that why 1==true?_ – DarkDust Feb 11 '15 at 14:35
  • Ok. I edited my question. But if guys read my question completely, they didn't give me negative point. This question is different with the questions guys sent me their links. – Amir Jalilifard Feb 11 '15 at 14:37
  • Truthy just means if you would do "if(myVar)" it would be true and Falsy the other way around...which comes down to the same as the difference between == and === – Ben A. Feb 11 '15 at 14:40
  • I know. The question is why ? why "hello" is true? What was the reason beyond this. – Amir Jalilifard Feb 11 '15 at 14:41
  • In particular, see [this answer](http://stackoverflow.com/a/359547/400056), which answer exactly why `1 == true`. Also see [this answer](http://stackoverflow.com/a/4923684/400056). The [truthy and falsy](http://www.sitepoint.com/javascript-truthy-falsy/) stuff is also easily found. – DarkDust Feb 11 '15 at 14:41
  • Because if you parse the String "hello" to a boolean it will return true. I'm not to 100% sure WHY they decided to do that but I assume it's because it makes it easier to verify whether a var is set. If you get for example a userinput it's nice to check if there is at least "something"...if that makes sense – Ben A. Feb 11 '15 at 14:43
  • See the documentation http://www.ecma-international.org/ecma-262/5.1/#sec-9.2 – R3tep Feb 11 '15 at 14:44
  • 1
    There's two answers to your _why_: because [the designers chose to specify it this way](http://stackoverflow.com/a/4923684/400056). But why they chose to specify it this ways is something we can only guess; it's useful in a lot of cases. – DarkDust Feb 11 '15 at 14:44
  • Please someone tell me, what was wrong with this question. There were no questions same as mine. There was no clear answer. This question also was unsolved. So, what was wrong with this question that I got -6 points? Nobody even read my question completely. Most of the guys just read 1 line of my question and gave negative point! – Amir Jalilifard Feb 11 '15 at 15:01

2 Answers2

6

JavaScript likes to convert values to other types implicitly whenever possible. Because of that, when comparing booleans to other types of variables, JavaScript uses the same logic as older programming languages. A value that represents empty, null, or zero (such as 0, or "") evaluates to false, and any other value (such as 1, 5, -19, "ghsfsah", or other meaningful content) evaluates to true.

Why does it do this? Well for one it allows developers a small shortcut when checking to see if a variable has content. For example, if a user doesn't give input in a text field, we can easily check to see if a field is empty and prompt the user.

if ( !textfield.value ) {
    alert( "Please enter something in the text box" );
}

If you need to see if something is actually true or false, you can use ===.

David Reeve
  • 863
  • 9
  • 16
  • 1
    Thanks. I just didn't understand why I got -6 points! Because I think someone should have asked this question! – Amir Jalilifard Feb 11 '15 at 14:58
  • 2
    @AmirJalilifard: I think the problem was that your question was understood as _why does the language consider "1" to be true_ (because it's specified this way) instead of _why did the designers of the language specify it this way_. I surely did interpret it in the first sense until you cleared it up. – DarkDust Feb 11 '15 at 15:01
  • @DarkDust Yes. Maybe It was not very clear at first but if guys kept reading up to the end, they would understand what I meant. You know, I can remember that once I asked a question about the differences between "Domain Specific language" and "Frameworks". Guys gave me -8 points just because they thought that question was not useful. But after 3 weeks they realized that question was important, however I lost my permission to ask question for 2 months. Anyway, thank you for your answer. Good luck – Amir Jalilifard Feb 11 '15 at 15:06
  • 1
    Never underestimate the ability of StackOverflow to misinterpret your question. Always be as clear as possible, and then be clearer. – David Reeve Feb 11 '15 at 15:08
2

JavaScript is very flexible about the types of values it requires. If JavaScript wants a boolean, it will convert whatever value you give it to a boolean.

Some values (“truthy” values) convert to true and others (“falsy” values) convert to false.

The following values convert to, and therefore work like, false:

  1. undefined
  2. null
  3. 0
  4. -0
  5. NaN
  6. "" // the empty string

All other values, including all objects (and arrays) convert to, and work like, true.

As an example, suppose that the variable o either holds an object or the value null. You can test explicitly to see if o is non-null with an if statement like this:

if (o !== null){
....
}

The strict not-equal operator !== compares o to null and evaluates to either true or false. But you can omit the comparison and instead rely on the fact that null is falsy and objects are truthy:

if (o){
....
}

In the first case, the body of the if will be executed only if o is not null. So the code block will execute even if o is set to undefined.

The second case is less strict: it will execute the body of the if only if o is not false or any falsy value (such as null or undefined). Which if statement is appropriate for your program really depends on what values you expect to be assigned to o. If you need to distinguish null from 0 and "", then you should use an explicit comparison.

RBT
  • 24,161
  • 21
  • 159
  • 240