Boolean
Boolean
A data type that holds only true/false values. The basis of conditionals and logical operations.
Boolean
A data type that holds only true/false values. The basis of conditionals and logical operations.
Boolean is a data type named after the English mathematician George Boole, and it can hold only two values: true and false. As one of the most fundamental data types in computer science, it forms the basis of all logical judgments and decision-making.
Boolean values play a key role in controlling program flow in conditional statements (if, while, for). The results of comparison operators (==, !=, >, <) and logical operators (&&, ||, !) are always returned as Boolean values, which allows complex conditions to be combined.
In most programming languages, Boolean is internally represented as 1 (true) and 0 (false). In JavaScript, there are the concepts of truthy and falsy: 0, "", null, undefined, and NaN are implicitly converted to false, and all other values to true. Understanding this characteristic is the key to writing bug-free code.
Boolean logic is used throughout practical work, including the WHERE clause of database queries, search engine filtering, permission checks, and feature flags. In AI/ML as well, Boolean arrays are used as a core element in the output of binary classification problems and in mask operations.
// 1. Boolean 기본 사용과 논리 연산
const isLoggedIn = true;
const isAdmin = false;
const hasPermission = true;
// 논리 연산자: AND(&&), OR(||), NOT(!)
const canEdit = isLoggedIn && hasPermission; // true (둘 다 참)
const canView = isLoggedIn || isAdmin; // true (하나만 참이면 됨)
const isGuest = !isLoggedIn; // false (반전)
console.log(`편집 가능: ${canEdit}, 조회 가능: ${canView}`);
// 2. 조건문에서의 Boolean 활용
function checkAccess(user) {
if (user.isActive && user.emailVerified) {
return '전체 접근 허용';
} else if (user.isActive) {
return '이메일 인증 필요';
} else {
return '계정 비활성화됨';
}
}
// 3. Truthy와 Falsy 이해하기
const falsyValues = [false, 0, '', null, undefined, NaN];
const truthyValues = [true, 1, 'hello', [], {}, -1];
falsyValues.forEach(val => {
console.log(`${String(val)}: ${Boolean(val)}`); // 모두 false
});
// 4. 단축 평가(Short-circuit Evaluation) 활용
const username = user?.name || '익명 사용자'; // falsy면 기본값
const settings = config ?? defaultConfig; // null/undefined면 기본값
// 5. Boolean 필터링
const items = [0, 1, false, 2, '', 3, null, 'hello'];
const truthyItems = items.filter(Boolean); // [1, 2, 3, 'hello']
console.log('Truthy 값들:', truthyItems);
// 6. 토글 패턴
let isDarkMode = false;
function toggleDarkMode() {
isDarkMode = !isDarkMode; // true ↔ false 전환
document.body.classList.toggle('dark', isDarkMode);
}
"I think we should manage this feature with a feature flag. With a single Boolean value, we can turn the feature on and off in production, so if a problem occurs, we can roll back immediately without deploying code."
"I understand the concepts of truthy and falsy in JavaScript. For example, an empty array [] is truthy because it is an object, but an empty string '' is falsy. So when checking whether an array is empty, I explicitly check with array.length > 0."
"Here, instead of `if (isValid == true)`, you can simply write `if (isValid)`. A Boolean variable is already true/false, so there's no need to compare it. Also, if you start variable names with the prefixes is, has, can, or should, you can tell right away that it's a Boolean."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
0, '', and false are all falsy, but they are different values. Checking with `if (!value)` also filters out 0 and empty strings, so check explicitly, as in `if (value === null || value === undefined)`, or use the `??` operator.
Use `if (flag)` instead of `if (flag === true)`. Likewise, `if (!flag)` is more idiomatic than `if (flag === false)`. However, when the value is not guaranteed to be a Boolean, an explicit comparison is safer.
Use prefixes that clearly indicate state, such as isEnabled, hasPermission, canEdit, and shouldUpdate. Vague names such as flag, check, and status reduce code readability.