I don’t know why the final output of the truthyness exercise is 1
In js, truthy is anything that when put into a comparator, will evaluate to true. 1 is truthy. If you do 1==true, it will eval to true.
I don’t get this yet
Hi SIMON_DAVILA,
It looks like you don’t understand the truthiness. Let me explain you.
The if statement can be either true
or false
depending on your test.
let x = 5;
if (x === 5) {
console.log('true!');
}
Since the test is always true, the code block of an if statement will run.
But what if we use an if statement that does not make a comparison like this?
let x = 5;
if (x) {
console.log('true!');
}
The code block of an if statement will also run. Weird!
This code always work because JS can be either truthy or falsy.
In other words, there are 6 falsy values:
-
0
: The number that is neither positive nor negative. -
false
: The boolean type that is opposite totrue
. -
''
: A string that has a length of 0, and no text inside the string. -
NaN
: Stands for Not a Number. Typically caused by some math errors. -
undefined
: A data type before it is defined. -
null
: A data type that can be assigned to a blank value.
All others are truthy.
Thanks!
Doge
But my initial question was why the output in this example is one?
Hi SIMON_DAVILA,
Remember that 1
is a truthy value. When you check that 1
is equal to true
, it will be evaluated to true
.
For 0
, 0
is a falsy (often called falsey) value. Once you check that 0
is equal to false
, it will evaluate to false
.
Hope this helps!
Doge