본문 바로가기

JS

JS - operator

JS 강의 필기 + 추가

 

dynamic typing 때문에 string concatenation 주의해야 됨

console.log('my' + ' cat'); // my cat
console.log('1' + 2);   // 12

numeric operators

add +

subtract -

divide /

multiply *

remainder %

exponentitation **


Increment and decrement operators 

++preIncrement, postIncrement++

--preDecrement, postDecrement--


assignment operators

let x = 3;
let y = 6;

x += y; // x = x + y
x -= y;
x *= y;
x /= y;

comparison operators

<, <=, >, >=

lt과 gt의 등호 위치가 앞인지 뒤인지 헷갈린다면 arrow function(=>)을 생각하자.


logical operators

|| (or), && (and), ! (not)

|| finds the first truthy value. && finds the first falsy value. 그러니 function이나 expression같이 heavy한 operation을 뒤에 배치하기.

Logical not ( ! )은 등호 앞 뿐만 아니라 value나 boolean primitive 앞에 붙여 쓸 수 있다.

Double not ( !! )은 뭐든 boolean primitive로 변환하도록 force하는 건데, value의 truthynessfalsyness를 기준으로 판단하기 떄문에 object는 비어있어도 true로 판단한다. falsy 8가지 false, 0, -0, 0n(bigInt), "", null, undefined, NaN

Logical not 참고 링크

 

often used to compress long if-statement

if (nullableObject != null) {
    nullableObject.sth;
}
// 위를 아래와 같이 쓸 수 있음
nullableObject && nullableObject.sth

equality

== loose equality with type conversion

console.log(stringFive == numberFive);  // true
console.log(stringFive != numberFive);  // false

 

=== strict equality without type conversion

console.log(stringFive === numberFive); // false
console.log(stringFive !== numberFive); // true

 

object equality by reference

const ellie1 = { name: 'ellie' };
const ellie2 = { name: 'ellie' };
const ellie3 = ellie1;

console.log(ellie1 == ellie2);  // false. reference가 다름
console.log(ellie1 === ellie2); // false
console.log(ellie1 === ellie3); // true

 

헷갈리는 거. null == undefined는 true

console.log(0 == false);   // true
console.log(0 === false);   // false. 0은 boolean type이 아님
console.log('' == false);   // true
console.log('' === false);  // false. ''는 boolean type이 아님
console.log(null == undefined);     // true. null은 undefined와 같은 것으로 간주됨
console.log(null === undefined);    // false. type이 다름

conditional operator: if

if ( condition1 ) { value1 } else if ( condition2 ) { value2 } else { value3 }


ternary operator: ?

condition ? value1 : value2;

간단할 때만 사용. condition이 true면 value1, false면 value2


switch statement

use for multiple if checks, enum-like(열거형) value check, and multiple type checks in TS

const browser = 'Firefox';
switch (browser) {
    case 'IE':
        console.log('go away!');
        break;
    case 'Chrome':
    case 'Firefox': // 안 되는 방식 -> case 'Chrome' || 'Firefox':
        console.log('love you!');
        break;
    default:
        console.log('same all!');
        break;
}

 

function calculate(command, a, b) {
    switch (command) {
    case 'add':
        return a + b;
    case 'substract':
        return a - b;
    case 'divide':
        return a / b;
    case 'multiply':
        return a * b;
    case 'remainder':
        return a % b;
    default:
        throw Error('unknown command');
    }
}

loops

loop 중첩하는 걸 nested loops 라고 하는데 O(n^2)라서 cpu에 좋지 않음

label loop는 현업에서 잘 사용하지 않음

 

while loop

while the condition is truthy, body code is executed.

let i = 3;
while (i > 0) {
    console.log(`while: ${i}`);
    i--;
}

 

do while loop

body code is executed first, then check the condition.

do {
    console.log(`do while: ${i}`);
    i--;
} while (i > 0);

 

for loop

for (begin; condition; step)

begin에 변수 선언이 들어가는 경우를 inline variable declaration라고 함

for (let i = 3; i > 0; i--) {
    console.log(`inline variable for: ${i}`);
}

 

continue

// iterate from 0 to 10 and print only even numbers (use continue)

for (let i = 0; i <= 10; i++) {
    if (i % 2 == 0) {
        console.log(`${i}`);
    } else {
        continue;
    }
}

 

break

// iterate from 0 to 10 and print numbers until reaching 8 (use break)

for (let i = 0; i < 11; i++) {
    if (i > 8) {
        break;
    }
    console.log(`${i}`);
}

 

 

 

'JS' 카테고리의 다른 글

JS - array  (0) 2020.12.24
JS - object  (0) 2020.12.23
JS - class  (0) 2020.12.22
JS - function  (0) 2020.12.21
JS - variable  (0) 2020.12.19