자바스크립트 세 번째 만남 (operator.js)

찬란·2022년 3월 30일
0
post-thumbnail

1. string concatenation

console.log('my' + 'cat'); = my cat
string + string = string
+기호를 사용해 문자열과 문자열을 합해서 새로운 문자열

console.log('1' + 2); = 12
string + number = string
문자열에 숫자를 더하게 되면 숫자가 문자열로 변환돼서 합해진다

console.log(string literals: 1 + 2 = ${1 + 2});
= string literals: 1 + 2 = 3

백팁 기호를 활용해서 string literals
변수 값을 계산해 string을 포함해서 문자를 만들 수 있다 number 계산은 ${}
string과 number 계산을 같이 쓰고 싶은 때 사용

2. Numeric operators

console.log(1 + 1); // add
console.log(1 - 1); // substract
console.log(5 / 2); // divide
console.log(parseInt(5 / 2)); // 몫
console.log(1 * 1); // multiply
console.log(5 % 2); // 나머지
console.log(2 ** 3); // 제곱

3. Increment and decrement operators

**let counter = 2;

const preIncrement = ++counter;
++counter; = counter에 +1 증가 후 변수에 넣어주기 때문에 counter와 변수 모두 3
= preIncrement : 3, counter : 3

const postIncrement = counter++;
counter++; = 먼저 변수에 3을 넣어준 후 counter 값을 증가시킨다
= preIncrement : 3, counter : 4

const preDecrement = --counter;
const postDecrement = counter--;
(마찬가지)

4. Assignment operators (할당 operators)

let x = 3;
let y = 6;

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

간단한 연산을 할 때 식을 줄여 쓸 수 있다

5. Comparison operators (비교 operators)

console.log(10 < 6); = false
console.log(10 <= 6); = false
console.log(10 > 6); = true
console.log(10 >= 6); = true

맞으면 true 틀리면 false

6. Logical operators (중요)

const value1 = false
const value2 = 4 < 2;

//||(or)

console.log(\or: ${value1 || value2 || check()});
console.log(\or: ${false || false || true}); = true

function check(){
for(let i=0; i<10; i++){
console.log('!!!!!');
}
return true;
}

or은 처음으로 true가 나오면 뒤 조건은 보지도 않고 멈춘다
value를 앞에 두고 무거운 function(함수) 연산은 제일 뒤로 연산을 두는 것이 좋음

// && (and)

console.log(and: ${value1 && value2 && check()});

and 또한 처음에 false가 도출되면 뒤는 연산하지 않으므로 무거운 연산은 맨 뒤로
null값인지 알아낼 때도 and를 쓰곤한다.

not 연산자

const value1 = false;

//!(not)
console.log(!value1);

값을 반대로 바꿔주는 연산자
true를 false로 false를 true로 바꾸는 연산자

7. Equality

const stringFive = '5';
const numberFive = 5;

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

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

Loose equality ( ==, != ) :느슨한 검사
숫자 5 문자 5 똑같은 5라고 인식하여 true로 출력됨

Strict equality ( ===, !== ) :엄격한 검사
숫자 5와 문자 5는 타입이 다르기 때문에 false로 출력됨

보통 Strict equality 사용

object의 경우 equality가 다르다

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

똑같은 이름을 가졌지만 ellie1과 ellie2는 가르키고 있는 주소가 다르다

console.log(ellie1 == ellie2); = false

console.log(ellie1 === ellie2); = false
console.log(ellie1 === ellie3); = true

8. if operator

const name = 'cgok';

if(name === 'ellie'){
console.log('Welcome, Ellie!');
}

else if(name === 'coder'){
console.log('You are amazing coder');
}

else{
console.log('unknown');
}

9. Tenary operator : ?

const name = 'cgok';
console.log(name === 'ellie' ? 'yes' : 'no'); = no

10. Switch statement

const browser = 'IE';
switch(browser) {
case "IE":
console.log("go away!!");
break;

case "Chrome":
case "FireFox":
console.log("love you!");
break;

default:
console.log("same all!");
break;
}

11. Loops : while

반복문

let i = 3

while(i > 0){
i가 0보다 큰지 확인 후 결과 출력

console.log(while: ${i});
i--;
}

결과
while: 3
while: 2
while: 1

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

블럭을 먼저 실행한 후 조건 검사
do while: 0

조건문이 맞을 때만 블럭 실행하고 싶을 때는 while
그냥 블럭을 먼저 실행하고 싶으면 do


12. for loop, for(begin; condition; step)

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

// begin은 딱 한번만 실행 i = 3

결과
for: 3
for: 2
for: 1

13. nested Loops

for(let i = 0; i<3; i++){ for(let j = 0; j<3; j++){ console.log(i: ${i}, j: ${j}); } }

결과
i: 0 j: 0
i: 0 j: 1
i: 0 j: 2

i: 1 j: 0
i: 1 j: 1
i: 1 j: 2

i: 2 j: 0
i: 2 j: 1
i: 2 j: 2

i가 한 번 돌 때마다 j가 3번 씩 도는 것을 확인할 수 있지만
cpu에 좋지 않기 때문에 피하는게 상책

profile
예비 프론트엔드 개발자

0개의 댓글