[JavaScript] operator

작성자
진행일시
2021/07/19
이메일

1. Operator 기본정리

// 1. String concatention console.log('my' + 'cat'); // (문자열과 문자열을 더할 수 있다 console.log('1' + 2); //문자열에 숫자를 더하면 숫자가 문자열로 변환 console.log(`string literals: 1 + 2 = ${1 + 2}`); //``기호 사용하면 변수값을 사용가능 console.log("ellie`s \n\tbook"); //2. Numeric Operators console.log(1 + 1); //add console.log(1 - 1); //substract console.log(1 / 1); //divide console.log(1 * 1); //multiply console.log(5 % 2) //remainder console.log(2 ** 3); //exponentiation(2의 3승) //3. Increment and decrement operators let counter = 2; const preIncrement = ++counter; //counter = counter + 1; //preIncrement = counter; console.log(`preIncrement : ${preIncrement}, counter: ${counter}`); const postIncrement = counter++; //postIncrement = counter; // counter = counter + 1; console.log(`postIncrement : ${postIncrement}, counter : ${counter}`); //4. Assignment operatorts let x = 3; let y = 6; console.log(x += y) ; // x = x + y; console.log(x -= y) ; // x = x - y; console.log(x *= y) ; // x = x * y; console.log(x /= y) ; // x = x / y; // 5 greter than, less than console.log(10 > 6); //6 Logical operators 중요하다 : || (or), &&(and), !(not) const value1 = false; const value2 = 4 < 2; // ||(or) true가 하나만 있으면 true , *진짜 중요한 point : or 연산자느 처음으로 ture가 나오면 거기서 멈춤* console.log(`or : ${value1 || value2 || check()}`); function check(){ for(let i = 0 ; i < 10; i ++){ //wasting time console.log('1'); } return true; } // &&(and) 모두 true 여야지 true , 그리고 heavy한 거는 항상 맨뒤 ex) 함수등등 // 그리거 null check 확인할 떄 많이 씀 console.log(`and : ${value1 && value2 && check()}`); function check(){ for(let i = 0 ; i < 10; i ++){ //wasting time console.log('1'); } return true; } //7. Equality const stringFive = '5'; const numberFive = 5; // == loose equality, 타입을 변경해서 검사 console.log(stringFive == numberFive); console.log(stringFive != numberFive); // === strict equality 타입이 다르면 달라! console.log(stringFive === numberFive); console.log(stringFive !== numberFive); // object eqality by reference const ellie1 = { name : 'ellie'}; const ellie2 = { name : 'ellie'}; const ellie3 = ellie1; console.log(ellie1 == ellie2); console.log(ellie1 === ellie2); console.log(ellie1 === ellie3 ); // eqiality - puzzler console.log( 0 == false); console.log(0 === false); console.log('' == false); console.log('' === false); console.log(null == undefined); console.log(null === undefined); //8 if, else if, else const name = 'aiden'; if(name === 'aiden') { console.log("welcome, aiden"); }else if (name === 'coder'){ console.log("sleep"); }else{ console.log("whooo!!!"); } // 9. Ternary oerator : ? // condition ? value1 : value2; console.log(name === 'aiden' ? 'yes' : 'no'); //10 switch const browser = 'IE'; switch(browser) { case 'IE': console.log('집가자'); break; case 'firefox': console.log('fox집가자'); break; case 'chrome': console.log('chrome집가자'); break; default : console.log('def집가자'); break; } // 11. Loops let i = 3; while(i > 0) { console.log(`while : ${i}` ); i--; } //12 do,while do { console.log(`do while : ${i}`); i--; } while(i > 0); //13 for for(i = 3 ; i > 0; i--){ console.log(`for : ${i}`); } for(let i = 3; i > 0; i = i -2) { //inline variable declaration console.log(`lnline variable for : ${i}`); } // nested loops for(let i = 0 ; i < 10 ; i++){ for(let j = 0; j < 10; j++) { console.log(`i : ${i}, j : ${j}`); } } //break, continue //Q1. iterate form 0 to 10 and print only even numbers(use continue); for(i = 0; i <= 10; i++) { if(i % 2 == 1 ) { continue; }else{ console.log(i); } } //Q2. iterate form 0 to 10 and print numbers until reaching 8 (use break); for(i = 0; i <= 10; i++){ if(i <=8 ){ console.log(i); }else{ break; } }
JavaScript
복사