Boolean
Overview
A Boolean is a data type that represents one of two values: true
or false
.
Key Characteristics
Basic Definition: A Boolean value is either
true
orfalse
. These values are typically the result of comparison operations or logical expressions.Type Conversion: Any value in JavaScript can be converted to a Boolean using the
Boolean()
function or double negation!!
. Examples:Boolean(0); // false Boolean(1); // true
1. **Common Use Cases:**
**Conditionals:** Boolean values are essential in `if` statements to control the flow of a program\.
```js
if (isAvailable) {
console.log("The item is available!");
}
**Logical Operators:** Boolean values work with operators like `&&`, `||`, and `!` for complex conditions\.
```js
let result = isAvailable && isAffordable;
1. **Truthiness and Falsiness:**
Some values in JavaScript are inherently "truthy" \(treated as `true`\) or "falsy" \(treated as `false`\)\. **Falsy values** include: `false`, `0`, `""`, `null`, `undefined`, and `NaN`\.
---
Last updated
Was this helpful?