Truthy and Falsy
Truthy Falsy
π¨βπΌ We often need to decide if a value "exists" without comparing it to
null or undefined. JavaScript treats some values as falsy, and everything
else as truthy.Falsy values:
false0,-0,0n''(empty string)nullundefinedNaN
You can convert any value to a boolean with
Boolean(value) or !!value.π¨ Open
and:
- Create
hasUsernamebased on the truthiness ofusername - Create
hasNicknamebased on the truthiness ofnickname - Create
hasAgebased on the truthiness ofage - Create
hasNotesbased on the truthiness ofnotes - Create
canCheckoutthat'struewhencartTotalis truthy andhasAcceptedTermsis true - Create
canCreateAccountthat'struewhenhasUsername,email, andpasswordare all truthy
π° Convert values to booleans when you need a strict true/false.
π MDN - Truthy
π MDN - Falsy