Problem Description
Write a function isTruthy(value) that takes any value.
It should return
trueif the value is truthy.It should return
falseif the value is falsy.
Constraint: You cannot use an if statement.
Test Cases:
isTruthy("hello")should returntrueisTruthy(42)should returntrueisTruthy(true)should returntrueisTruthy("")should returnfalseisTruthy(0)should returnfalseisTruthy(undefined)should returnfalse
💡 The 'Syntax Nudge'
This is the classic use case for the double bang (!!) operator. How can you use it to "normalize" the input value into its pure boolean form?
If ! means "take this value, find its truthiness, and flip it," then !! simply means "do that twice."
!!value=!(!value)
Why on earth would you do this? It's used to normalize any value into a "pure" boolean. It's a very common way to answer the question, "Does this value have anything in it?"
Let's trace it with "hello":
!"hello"(truthy) becomesfalse!false(the second!) becomestrueSo,!!"hello"=true
Let's trace it with 0:
!0(falsy) becomestrue!true(the second!) becomesfalseSo,!!0=false
The !! operator is a concise way of forcing any value to its simple true or false equivalent.