![]() |
VOOZH | about |
To represent logical values, JavaScript uses the Boolean data type, which has two possible values: true or false. These values often result from comparisons or logical operations. Additionally, the Boolean() function can convert other types of values into Boolean, determining their truthy or falsy nature.
The Boolean() function is used to explicitly convert a value to its Boolean equivalent. Truthy values return true, while falsy values return false.
true false
In JavaScript, all objects, arrays, and non-empty strings are considered truthy values.
true true true
Objects and arrays always evaluate to true in Boolean contexts.
Certain values, like 0, null, undefined, NaN, and empty strings, are falsy.
true false
These values lack meaningful content and thus evaluate to false.
While JavaScript supports primitive Booleans, the Boolean constructor can create Boolean objects. However, these objects are truthy regardless of their value.
[Boolean: false] object true
Using new Boolean() is generally discouraged because it can lead to confusion.
A Boolean primitive is a simple representation of true or false. This is the most commonly used Boolean type in JavaScript.
boolean
Boolean primitives are lightweight and efficient for logical operations.
JavaScript implicitly converts values to Boolean in contexts like conditions and loops.
Invalid input
Empty strings evaluate to false, so "Invalid input" is logged.
The Boolean constructor can create Boolean objects, but primitive Booleans are preferred.
boolean object
Primitive Booleans are easier to use and avoid unnecessary complexity.
Falsy Values: Values that are evaluated as false when used in a Boolean. Unlike truthy values, falsy values represent "nothingness," "emptiness," or "failure."
Truthy Values: Values that are evaluated to be true when used in a Boolean context, such as in conditional statements or logical operations.
Truthy/Falsy in Action
false true false true
Age missing
Since userAge is 0, it evaluates to falsy, and "Age missing" is logged.
Pass
false
Guest
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through