1

While programming with javascript, I come in some situations that I have a condition an wonder what values will pass that condition (if it will be truthy).

The solution I came up with is open the console in chrome and type if(foo) {true} and if the foo is truthy, it returns true, otherwise false.

An example of that is when I have some expression (foo) that won't return only true/false values. Inside the if it can return alot of things depending on the input (sometimes string, or number or maybe NaN).

But I fell that writing a if for that is too much.

Is there a easiest way of checking the truthy of a value in javascript?

Edit:

I'm looking for the easiest way of checking the truthy of a value, not just how to check and as I said in my question, I already do if(foo) {true} to check, but I'm looking for a easiest way

Vencovsky
  • 28,550
  • 17
  • 109
  • 176

1 Answers1

-2

I would use a ternary operator to shorthand the if-statement.

The result is something like this:

const foo = true;
console.log(foo ? 'truthy' : 'falsy'); // returns truthy
StefanN
  • 871
  • 6
  • 19