Questions tagged [falsy]

19 questions
11
votes
2 answers

What is the difference between truthy and falsy with true and false in JavaScript?

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…
Amir Jalilifard
  • 2,027
  • 5
  • 26
  • 38
4
votes
1 answer

Why destructuring assignment doesn't know null value as falsy and use default value?

Assume we have a function that use some keys in the inner object of argument: const api = ({ data: { name } = {} }) => `My name is ${name}.`; If we pass {}, { data: '' }, { data: 0 }, { data: NaN } or { data: undefined } to the function we will…
AmerllicA
  • 29,059
  • 15
  • 130
  • 154
3
votes
3 answers

Removing all falsy values from a javascript array, including NaN but excluding 0 and empty strings

I have the following array: const values = ['', 0, 'one', NaN, 1, 'two', 2, null, 'three', undefined, 3, false]; I would like to filter() out all the falsy values except for '' and 0. I understand there is useful shorthand: return…
Rounin
  • 27,134
  • 9
  • 83
  • 108
2
votes
2 answers

How to check if variable is not falsy but 0 passes in Javascript

Is there an elegant way to check if variable is NOT falsy but in case of 0 it passes. The issue with this way of verifying if(var !== undefined && var !== null) is that it's long and doesn't cover all cases like undecalred or NaN. I'm also using…
valentin Ivanov
  • 83
  • 1
  • 10
1
vote
3 answers

Operator to test for falsy values with zero treated as truthy

Is there a minimalistic syntax to do the same as x === 0 ? true : !!x. The goal of this expression is to avoid the exclusion of zero as falsy and yet make sure other falsy values do are converted to false.
Akheloes
  • 1,352
  • 3
  • 11
  • 28
1
vote
2 answers

How can I check this function for truthy or return an empty array if falsy?

I am looking at this assignment, and my question is about the part in bold at the end: // Do not edit the code below. var myGroceryList = ['chips', 'pizza', 'hotpockets', 'MtnDew', 'corndogs']; // Do not edit the code above. Here we're going to…
1
vote
1 answer

Handsontable 7.4 dropdown cell with falsy value (0) shows placeholder

I am looking for a way to display the numeric value 0 in a dropdown list that also includes placeholder text when the cell is empty. Currently, if 0 is selected the placeholder text shows through. I'm hoping for a built-in option and I'd like to…
DevJem
  • 656
  • 1
  • 13
  • 21
1
vote
2 answers

This condition returns a boolean when checking for a falsy, but it returns a string when checking for a truthy value. How can I get it to work?

I get a boolean return when checking for a falsy value, but not a boolean when checking for a truthy value... I am just getting a string instead. This works. const isFalse = !values.firstName && !values.lastName && !values.email; But this doesn't,…
Grateful
  • 9,685
  • 10
  • 45
  • 77
0
votes
0 answers

In a liquid template, how can I differentiate false from null?

In JavaScript, we can use strict equality (===, !==, etc.) to determine if something is truly false instead of just falsy. I have several nullable booleans in a model that is being passed into a liquid template, and the only mechanism I can come up…
user15716642
  • 171
  • 1
  • 12
0
votes
1 answer

Coerce any nullish var into a bool in one command

In JavaScript we can coerce any variable into a boolean using either !!myVar or Boolean(myVar). But these coercions are based on whether the value is falsy. I wanted to know if there's a way to make it based on whether the value is nullish. I know…
0
votes
2 answers

How to return a new array with all truthy values removed? (Javascript)

I have seen how to remove falsy values from an array but haven't found a solution for returning a new array with all truthy elements removed. I attempted to replace falsy values with truthy ones in my "solution" but the results are not leaving me…
la10nay
  • 35
  • 3
0
votes
1 answer

Ternary operator rendering nothing in React when variableis equal to 0

I am trying to render a React component on click of another component. The onClick I set a savedId to be the eventKey of the component to render the correct data related to that specific index the array. onClick call
melly18
  • 87
  • 3
  • 11
0
votes
0 answers

Why the empty object { } and the empty array [ ] aren’t falsy, how they are truthy, by definition

ref:https://medium.com/coding-at-dawn/what-are-falsy-values-in-javascript-ca0faa34feb4 by Dr. Derek Austin Since I have started my development career with JavaScript this question always arise in mind how?? Truthy values include the empty object {}…
0
votes
2 answers

Check for falsy values on array

I don't understand why this piece of code works can someone explain to me? If I delete this piece of the conditional && arr[i] the value of arr[5] don't assume as a falsy value, but if I write that piece of code already assumes arr[5] like a falsy…
0
votes
1 answer

Is NaN always a falsy value in Javascript?

console.log(typeof 3/0, Boolean(3/0)); // This returns NaN true console.log(typeof 0/0, Boolean(0/0)); // This returns NaN false I thought both will return NaN false in the Console as NaN is falsy value. But this is not happening. Can someone…
prabhakar
  • 385
  • 1
  • 2
  • 8
1
2