0

A quick intro, I am a total noob learning JS and feel that it's going well, however I am doing a simple exercise right now and I'm hung up on something.

I have learned that: a falsey value is a value that is considered false when encountered in a boolean context ex: false, 0, -0, 0n, "", null, undefined, NaN (Not a number) truthy is everything other than falsey (such as a String, boolean true, any number not 0 etc.)

so in my example below, if anyone could help me understand why value => value == true, would print out false (as was the case) when I have a string value in my array ("Angela"). Thanks!

let values = [11, NaN, [], "Angela"]

function checkForFalsey() {
  if (values.some(value => value == true)) {
    console.log("At least one item is falsey")
  }
}
checkForFalsey()
  • 2
    `some` will return as soon as the condition is true. It won't reach upto `Angela`. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some – DecPK May 21 '21 at 00:10
  • Also note that not all falsy values are considered equal to `false`, even when using sloppy equality (`==`/`!=`) – Aluan Haddad May 21 '21 at 00:16
  • 1
    You ask "in my example below, if anyone could help me understand why value => value == true, would print out false" but your example shows `value => value == false`. So... what is your question? – Heretic Monkey May 21 '21 at 00:18
  • Interesting, but then since my first value in the array is an integer why would I still get a false value? – excelwinterfresh May 21 '21 at 00:19
  • Because truthy and true or false are not the same. The number is not == false so will immediately return. Do some more reading on how to test truthy/falsy – charlietfl May 21 '21 at 00:20
  • Because, even though the array contains falsy elements, it does not contain any elements for which `element == false` is truthy – Aluan Haddad May 21 '21 at 00:21
  • 2
    Nothing in your code "prints out false". Please provide code that exactly reproduces the result you are finding confusing. – Heretic Monkey May 21 '21 at 00:22
  • Charlietfl's answer makes the most sense. I think I got it. Thanks – excelwinterfresh May 21 '21 at 00:23
  • Heretic Monkey - I edited my code, now it says true. Hope it makes more sense now, I wanted to know why it printed false when I thought a 11, or Angela would be a truthy value and so I was confused why I saw false printed out. But, like I said I think I got it now. – excelwinterfresh May 21 '21 at 00:27

2 Answers2

0

A string is neither truthy nor falsy. What's happening is called type coercion. Since string and Boolean are not the same type, JavaScript coerces one of the types to match the other then checks equality (a high level explanation). See here for a detailed explanation: https://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/

So when you do "abc" == true JavaScript is coercing both values to a number type. A string is not a number, so toNumber("abc") returns NaN. It also coerces true to a number, which returns 1 in this case (1 is true, 0 is false, as you mentioned).

For what it's worth, NaN is also not truthy or falsy. Comparing it to true or false will always result in false.

Matt U
  • 4,970
  • 9
  • 28
  • 1
    Added detail: `NaN` is not equal to anthing, not even itself: `NaN==NaN` and `NaN===NaN` are both false. – some May 21 '21 at 01:21
-2

Edit:

The question was a bit confusing because of the snippet, I understood that you were trying to look for falsy values.

The reason why:

value => value == true

would print out false it's because none of the elements of the array is equal to true.

You are correct about what a falsy value is, but that doesn't mean that a truthy value would be == to true.

Here you can read more about it:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

One way of checking for truthy values would be:

(values.some(value => value != false)) 

or

(values.some(value => !!value === true)) 

Old answer:

Because the method you use tests that at least one element in the array matches the condition.

You can read more here

If you want to check that all elements of the array matches the condition, then you can use .every()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

let values = [11, NaN, [], ""]

function checkForFalsey() {
  if (values.every(value => value == false)) {
    console.log("All values are falsey");
  } else {
    console.log("NOT all values are falsey");
  }
}
checkForFalsey()
Saissaken
  • 87
  • 4
  • 2
    The original question is a bit confusing, but this isn't the correct answer as I understand it. If OP uses `value == true` for the `some(...)` condition, the result is still false and they want to know why. – Matt U May 21 '21 at 00:18
  • Thanks! I missunderstood the question because of the snippet. So I have updated the answer. – Saissaken May 21 '21 at 00:28