Operator - wooyeonhui/Yeonny GitHub Wiki
๋์ ์ฐ์ฐ์
Example Same As ใ กใ กใ กใ กใ กใ กใ กใ กใ ใ กใ กใ กใ กใ กใ ก
x = y x = y x+=y x = x + y x-=y x = x - y x*=y x = x * y x/=y x = x / y x%=y x = x % y
๋น๊ต ์ฐ์ฐ์(Comparison Operators)
Operator Description == ๋๋ฑ๋น๊ต(loose equality) ํ๋ณํ ํ, ๋น๊ตํ๋ค. === ์ผ์น๋น๊ต(strict equality) ํ์ ๊น์ง ์ผ์นํ์ฌ์ผ true๋ฅผ ๋ฐํํ๋ค. != !==
<
= <= ? ์ผํญ ์ฐ์ฐ์
//์ผํญ์ฐ์ฐ์ (ternary operator) //์กฐ๊ฑด ? ์กฐ๊ฑด์ด true์ผ๋ ๋ฐํํ ๊ฐ : ์กฐ๊ฑด์ด false์ผ๋ ๋ฐํํ ๊ฐ var condition = true; var result = condition ? 'true' : 'false'; console.log(result); //'true'
// id์ ๊ธธ์ด๊ฐ INPUT_ID_MIN_LEN๋ณด๋ค ์์ผ๋ฉด ์๋ฌ ๋ฉ์ธ์ง๋ฅผ ์ถ๋ ฅํ๋ค. var id = 'lee'; var INPUT_ID_MIN_LEN = 5; var errMsg = id.length < INPUT_ID_MIN_LEN ? '์์ด๋๋ 5์๋ฆฌ ์ด์์ผ๋ก ์ ๋ ฅํ์ธ์' : '์ฑ๊ณต'; console.log(errMsg); // '์์ด๋๋ 5์๋ฆฌ ์ด์์ผ๋ก ์ ๋ ฅํ์ธ์'
๋ ผ๋ฆฌ ์ฐ์ฐ์(Logical Operators)
๋ ผ๋ฆฌ ์ฐ์ฐ์๋ Boolean ๊ฐ๊ณผ ํจ๊ป ์ฌ์ฉํ์ฌ Boolean ๊ฐ์ ๋ฐํํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ด๋ค. ์ฌ์ค ๋ ผ๋ฆฌ ์ฐ์ฐ์๋ ํผ์ฐ์ฐ์ ์ค ํ๋๋ฅผ ๋ฐ๋ณตํ๋ค.
|| OR && AND ! NOT
๋จ์ถ ํ๊ฐ(Short-Circuit Evaluation)
๋ ผ๋ฆฌ ์ฐ์ฐ์๊ฐ Boolean ๊ฐ๊ณผ ํจ๊ป ์ฌ์ฉ๋์ง ์์ ๊ฒฝ์ฐ, Boolean ๊ฐ์ ๋ฐํํ์ง ์์ ์ ์๋ค. ์ด๋ ๋ ผ๋ฆฌ ์ฐ์ฐ์๊ฐ ํผ์ฐ์ฐ์ ์ค ํ๋๋ฅผ ๋ฐํํ๊ธฐ ๋๋ฌธ์ด๋ค. ๋ ผ๋ฆฌ ์ฐ์ฐ์๋ ๋ค์์ ๊ท์น์ ๋ฐ๋ผ์ "๋จ์ถ ํ๊ฐ"๋ก ๊ฒ์ฌ๋๋ค.
ํ๊ฐ์ ํ๊ฐ๊ฒฐ๊ณผ
true || anything true false || anything anything true && anything anything false && anything false
Boolean ๊ฐ์ผ๋ก ํ๊ฐํ๊ธฐ ์ํด ์ฐธ์กฐํ์ฌ์ผ ํ ๊ณณ๊น์ง ์งํํ ํ, ํ๊ฐ๋ฅผ ์ค์งํ๊ฒ๋ ๊ณ๊ธฐ๊ฐ ๋ ๊ฐ์ ๋ฐํํ๋ค.
var foo = 'Cat' && 'Dog' // t && t return 'Dog'
์ด๊ฒฝ์ฐ, 'Cat'์ true๋ก ํ๊ฐ๋๋ฏ๋ก ์ฐ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์๊ธฐ ์ํด์๋ 'Dog'๊น์ง ํ๊ฐํด ๋ณด์์ผ ํ๋ค. ๋ฐ๋ผ์ ํ๊ฐ๋ฅผ ์ค์งํ๊ฒ๋ ๊ณ๊ธฐ๊ฐ ๋ ๊ฐ('Dog')์ ๋ฐํํ๋ค.
var foo = false && 'Cat' // f && t returns false
์ด ๊ฒฝ์ฐ, false ๊ฐ ์ฒ์ ๋ฑ์ฅํ์๋ค. ๋ ์ด์ ์งํํ์ง ์์๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ ์ ์์ผ๋ฏ๋ก ํ๊ฐ๋ ์ค์ง๋๊ณ ํ๊ฐ๋ฅผ ์ค์งํ๊ฒ๋ ๊ณ๊ธฐ๊ฐ ๋ ๊ฐ์ธ false๊ฐ ๋ฐํ๋๋ค.
var foo = 'Cat' || 'Dog' // t || t returns 'Cat'
์ด ๊ฒฝ์ฐ, 'Cat'์ true๋ก ํ๊ฐ๋๋ฏ๋ก ํ๊ฐ๋ ์ค์ง๋๊ณ Cat์ด ๋ฐํ๋๋ค.
// || (๋ ผ๋ฆฌ ํฉ) ์ฐ์ฐ์ var o1 = 'Cat' || 'Dog'; // t || t returns Cat var o2 = false || 'Cat'; // f || t returns Cat var o3 = 'Cat' || false; // t || f returns Cat
// && (๋ ผ๋ฆฌ๊ณฑ) ์ฐ์ฐ์ var a1 = 'Cat' && 'Dog'; // t && t returns Dog var a2 = false && 'Cat'; // f && t returns false var a3 = 'Cat' && false; // t && f returns false
// example function foo (str) { str = str || ''; // do somethig with str console.log(str.length); }
foo(); // 0 foo('hi'); // 2
// example var obj = { // foo: 'hi', bar: 'hey' };
console.log('obj.foo is ' + obj.foo); // obj.foo is undefined
if (obj && obj.foo) { // do somethig with obj.foo console.log('obj.foo is ' + obj.foo); }
ํ์ ์ฐ์ฐ์ (Type Operators)
Operator Description typeof ๋ณ์์ ์๋ฃํ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ค. null๊ณผ ๋ฐฐ์ด์ ๊ฒฝ์ฐ object,ํจ์์ ๊ฒฝ์ฐ function๋ฅผ ๋ฐํํ๋ ๊ฒ์ ์ ์ํ์ฌ์ผ ํ๋ค. instanceof ๊ฐ์ฒด๊ฐ ๋์ผ ๊ฐ์ฒดํ์ ์ธ์คํด์ค์ด๋ฉด true๋ฅผ ๋ฐํํ๋ค.
typeof 'John' // returns string typeof 3.14 // returns number typeof NaN // returns number typeof false // returns boolean typeof [1, 2, 3, 4] // returns object typeof {name:'John', age:34} // returns object typeof new Date() // returns object typeof function () {} // returns function typeof myCar // returns undefined (์ค๊ณ์ ๊ฒฐํจ) typeof null // returns object (์ค๊ณ์ ๊ฒฐํจ)
function Person(){} var me = new Person() me instanceof Person // returns true
!!
feel like ์๋ค ์๋ค
console.log(!!1); // true console.log(!!0); // false console.log(!!'string'); // true console.log(!!''); // false console.log(!!null); // false console.log(!!undefined); // false console.log(!!{}); // true console.log(!![]); // true
๊ฐ์ฒด(๋ฐฐ์ด ํฌํจ)์ ๊ฒฝ์ฐ ๋น ๊ฐ์ฒด๋ผ๋ ์กด์ฌํ๊ธฐ๋งํ๋ฉด true๋ก ๋ณํ๋๋ค. ๊ฐ์ฒด์ ์กด์ฌ ํ์ธ ํ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํด์ผ ํ๋ ๊ฒฝ์ฐ, !!๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ์ ๋ก ํผ์ฐ์ฐ์๋ฅผ boolean์ผ๋ก ํ ๋ณํ ํ ์ ์๋ค.
var obj; console.log(!!obj); //false
obj = {}; console.log(!!obj); //true