์กฐ๊ฑด๋ฌธ์ ํ๊ฐ ๋ฐฉ์
ํ์ ์คํฌ๋ฆฝํธ๋ก ์๋ฃ๊ตฌ์กฐ๋ฅผ ๊ตฌํํ๊ณ ์๋๋ฐ, ๋ค๋ฅธ ์์์์ node์ next ๊ฐ์ด ์กด์ฌํ๋ฉด ์์ ํ๋ ๊ทธ๋ฐ ๋ก์ง์ด ์์๋ค.
?!!?!?!?!?!?!?!?!?!?!?!
if (node.next !== null) {
//์ด๊ฒ ์๋๊ณ ์๋๊ฐ ๋๋ค๊ณ ?
}
if (node.next) {
node.next.prev = node.prev;
}
node๋ ListNode<TValue> | null ์ผ๋ก ํ์ ์ ์ถ๋ก ํ ์ ์์๋ค. ๊ทผ๋ฐ if () ์กฐ๊ฑด๋ฌธ์ boolean ๊ฐ์ด ์๋ ๊ฐ์ฒด๋ null ์ด ๋ค์ด๊ฐ๋ค๋ ๊ฒ์ ์ถฉ๊ฒฉ์ ๋ฐ์๋ค. ์ ์ ๊ฒ ์ปดํ์ผ ์๋ฌ๊ฐ ์์๊ธฐ์ง?!
์ฐพ์๋ณด๋ ์๋ฐ์คํฌ๋ฆฝํธ๋ ์กฐ๊ฑด๋ฌธ ์์๋ ๋ฐ๋์ boolean ๊ฐ๋ง ๋ค์ด๊ฐ์ผ ํ๋ค๋ ๊ท์น์ด ์กด์ฌํ์ง ์์๋ค.(๋ ธ๊ทผ๋ณธ... ์๋๋๊ณ ....)
TypeScript(๋๋ JavaScript)์์ ์กฐ๊ฑด๋ฌธ(if)์ ๋ด๋ถ์ ์ผ๋ก ๊ฐ์ truthy ๋๋ falsy ์ฌ๋ถ๋ฅผ ํ๊ฐํ์ฌ ์คํ๋๋ค. ๋ฐ๋ผ์ ๊ฐ์ฒด, null, undefined ๋ฑ์ด ๋ค์ด์ฌ ์ ์๋ค.
Truthy ๊ฐ
if ์กฐ๊ฑด์ด truthy๋ก ํ๊ฐ๋๋ฉด ๋ธ๋ก ๋ด๋ถ์ ์ฝ๋๊ฐ ์คํ๋๋ค.
Truthy๋ก ํ๊ฐ๋๋ ๊ฐ
- ๊ฐ์ฒด ({}, [], ํจ์ ๋ฑ)
- ๋ฌธ์์ด ("string", " " ๋ฑ, ๋น ๋ฌธ์์ด "" ์ ์ธ)
- ์ซ์ (1, -1, Infinity ๋ฑ, 0 ์ ์ธ)
- true
Falsy ๊ฐ
if ์กฐ๊ฑด์ด falsy๋ก ํ๊ฐ๋๋ฉด ๋ธ๋ก ๋ด๋ถ์ ์ฝ๋๋ ์คํ๋์ง ์๋๋ค.
Falsy๋ก ํ๊ฐ๋๋ ๊ฐ
- null
- undefined
- false
- 0
- NaN
- ๋น ๋ฌธ์์ด ("")
๊ฒฐ๋ก
if (node.next) {
node.next.prev = node.prev;
}
๋ฐ๋ผ์ ์ ์กฐ๊ฑด๋ฌธ์ ๊ฒฐ๊ตญ node.next๊ฐ truthy์ผ ๋ ์กฐ๊ฑด๋ฌธ์ด ์คํ๋๋ค.
์ฆ ListNode<TValue> ํ์ ์ผ๋, ๊ฐ์ฒด์ธ ๊ฒฝ์ฐ ์กฐ๊ฑด๋ฌธ์ด ์คํ๋๋ค.
node.next๊ฐ null ๋๋ undefined์ผ ๊ฒฝ์ฐ, ์กฐ๊ฑด๋ฌธ์ ์คํ๋์ง ์๋๋ค.

ECMAScript ์ฌ์์ ๋ฐ๋ฅธ ์กฐ๊ฑด๋ฌธ ํ๊ฐ ๊ณผ์
- ์กฐ๊ฑด์ ํ๊ฐ
- ์กฐ๊ฑด์์ด Expression์ผ๋ก ํ๊ฐ๋๋ค.
- ์ด ํ๊ฐ๋ ๊ฒฐ๊ณผ๋ GetValue๋ฅผ ํตํด ์ป๋๋ค.
- ์ถ์ ์ฐ์ฐ ToBoolean ์ํ
- ์กฐ๊ฑด์์ ๊ฒฐ๊ณผ ๊ฐ์ ๋ด๋ถ์ ์ผ๋ก ToBoolean ์ถ์ ์ฐ์ฐ์ ์ํด ์ฒ๋ฆฌ๋๋ค.
- ToBoolean์ ๊ฐ์ truthy ๋๋ falsy๋ก ๋ณํํ๋ค.
- ๊ฒฐ๊ณผ์ ๋ฐ๋ผ ๋ถ๊ธฐ ์คํ
- ToBoolean ๊ฒฐ๊ณผ๊ฐ true์ด๋ฉด if ๋ธ๋ก์ ์คํ
- false์ด๋ฉด else ๋ธ๋ก(๋๋ ์๋ฌด๊ฒ๋ ์คํํ์ง ์์)
ECMAScript ์ฌ์์ ํ๋ด๋ธ JavaScript ์ฝ๋
function toBoolean(value) {
switch (value) {
case undefined:
case null:
case false:
case 0:
case -0:
case NaN:
case "":
return false; // Falsy ๊ฐ
default:
return true; // Truthy ๊ฐ
}
}
// ์กฐ๊ฑด๋ฌธ ํ๊ฐ ๋ก์ง ํ๋ด
function evaluateIfCondition(condition) {
const booleanValue = toBoolean(condition);
if (booleanValue) {
console.log("Condition is truthy");
} else {
console.log("Condition is falsy");
}
}
// ํ
์คํธ
evaluateIfCondition(null); // Condition is falsy
evaluateIfCondition(1); // Condition is truthy
evaluateIfCondition(""); // Condition is falsy
evaluateIfCondition({}); // Condition is truthy
evaluateIfCondition(undefined); // Condition is falsy

https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toboolean
ECMAScript® 2025 Language Specification
Introduction This Ecma Standard defines the ECMAScript 2025 Language. It is the sixteenth edition of the ECMAScript Language Specification. Since publication of the first edition in 1997, ECMAScript has grown to be one of the world's most widely used gener
tc39.es
'JavaScript Programming' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JavaScript ์ผ๋ฐ ํจ์์ ํ์ดํ ํจ์์ ์ฐจ์ด์ ๋ฌธ๋ฒ (0) | 2024.12.20 |
---|---|
JavaScript - ์คํ ์ปจํ ์คํธ์ ๋ ์์ปฌ ํ๊ฒฝ (0) | 2024.12.19 |
JavaScript์์ 1 < x < 3์ด ํญ์ ์ฐธ์ธ ์ด์ - Number ๋ด์ฅ ํจ์ (0) | 2024.12.17 |
๋๊ธ