๐ŸŸฉ ๋…ผ๋ฆฌ์  AND(&&)


๋…ผ๋ฆฌ์  AND (&&)์€ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ‰๊ฐ€ํ•˜๋ฉด์„œ ์ฒซ ๊ฑฐ์ง“ ๊ฐ™์€ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๋งŒ๋‚˜๋ฉด ์ฆ‰์‹œ ๊ทธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ชจ๋“  ๊ฐ’์ด ์ฐธ ๊ฐ™์€ ๊ฐ’์ด๋ผ๋ฉด ๋งˆ์ง€๋ง‰ ํ”ผ์—ฐ์‚ฐ์ž์˜ ๊ฐ’์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.
๋งŒ์•ฝ ์–ด๋–ค ๊ฐ’์ด true๋กœ ๋ณ€ํ™˜ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ๊ทธ ๊ฐ’์€ ์†Œ์œ„ ์ฐธ ๊ฐ™์€ ๊ฐ’(truthy)์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์–ด๋–ค ๊ฐ’์ด false๋กœ ๋ณ€ํ™˜ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ๊ทธ ๊ฐ’์€ ์†Œ์œ„ ๊ฑฐ์ง“ ๊ฐ™์€ ๊ฐ’(falsy) ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

result = '' && 'foo';  // result ์— ""(๋นˆ ๋ฌธ์ž์—ด)์ด ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค
result = 2 && 0;       // result ์— 0 ์ด ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค
result = 'foo' && 4;   // result ์— 4 ๊ฐ€ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.

 

a1 = true && true // t && t returns true
a2 = true && false // t && f returns false
a3 = false && true // f && t returns false
a4 = false && (3 === 4) // f && f returns false
a5 = 'Cat' && 'Dog' // t && t returns "Dog"
a6 = false && 'Cat' // f && t returns false
a7 = 'Cat' && false // t && f returns false
a8 = '' && false // f && f returns ""
a9 = false && '' // f && f returns false




https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Logical_AND

 

๋…ผ๋ฆฌ์  AND (&&) - JavaScript | MDN

๋…ผ๋ฆฌ์  AND (&&) (๋…ผ๋ฆฌ์  ์—ฐ๊ฒฐ) ์—ฐ์‚ฐ์ž๋Š” ๋ชจ๋“  ๋ถˆ๋ฆฌ์–ธ ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ true๊ฐ€ ๋˜์—ˆ์„ ๋•Œ ํ•ด๋‹น ํ”ผ์—ฐ์‚ฐ์ž์˜ ์ง‘ํ•ฉ์€true๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

developer.mozilla.org


๐ŸŸฉ ๋…ผ๋ฆฌ์  OR(||)

 

If x can be converted to true, returns x; else, returns y.

If a value can be converted to true, the value is so-called truthy. If a value can be converted to false, the value is so-called falsy.

 

 
true || true; // t || t returns true
false || true; // f || t returns true
true || false; // t || f returns true
false || 3 === 4; // f || f returns false
"Cat" || "Dog"; // t || t returns "Cat"
false || "Cat"; // f || t returns "Cat"
"Cat" || false; // t || f returns "Cat"
"" || false; // f || f returns false
false || ""; // f || f returns ""
false || varObject; // f || object returns varObject

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR

 

Logical OR (||) - JavaScript | MDN

The logical OR (||) (logical disjunction) operator for a set of operands is true if and only if one or more of its operands is true. It is typically used with boolean (logical) values. When it is, it returns a Boolean value. However, the || operator actual

developer.mozilla.org


โœ๏ธ or and๋ฅผ ๊ฐ™์ด ์“ธ ๋•Œ ์šฐ์„ ์ˆœ์œ„๋Š”?

 

"&&" >> "||"   

 

“&&”   - > ์ด๋†ˆ์ด ๋” ์šฐ์„ ์ˆœ์œ„ ๋†’์Œ

 

 
true || false && false; // returns true, because && is executed first
(true || false) && false; // returns false, because grouping has the highest precedence

 

'IT > JavaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JS] โœ๏ธ Infinity / ifFinite()  (0) 2023.07.17
[JS] โœ๏ธ NaN (Not a Number) / isNaN()  (0) 2023.07.17
[JS] โœ๏ธ Nullish (??)  (0) 2023.07.17
[JS] โœ๏ธ Truthy && Falsey  (0) 2023.07.17
[JS] โœ๏ธ eval(), JSON.parse(), JSON.stringify()  (0) 2023.07.17

+ Recent posts