-
3. operator, if, for loopJavaScript & TypeScript 2020. 11. 13. 17:40
1. String concatenation
console.log('my' + ' cat'); // my cat console.log('1' + 2); // 12 console.log(`string literals: 1 + 2 = ${1 + 2}`); // 1 + 2 = 3
2. Numeric operators
console.log(1 + 1); // add : 2 console.log(1 - 1); // substract : 0 console.log(1 / 1); // divide : 1 console.log(1 * 1); // multiply : 1 console.log(5 % 2); // remainder : 1 console.log(2 ** 3); // exponentiation : 8
3. Increment and decrement operators
let counter = 2; const preIncrement = ++counter; // 3, 3 const postIncrement = counter++; // 3, 4 const preDecrement = --counter; // 3, 3 const postDecrement = counter--; // 3, 2
4. Assingment operators
let x = 3; let y = 6; x += y; x -= y; x *= y; x /= y;
5. Comparison operators
console.log(10 < 6); // less than console.log(10 <= 6); // less than or equal console.log(10 > 6); // greater than console.log(10 >= 6); // greater than or equal
6. Logical operators : || (or), && (and), ! (not)
const value1 = true; const value2 = 4 < 2; // || (or), finds the first truthy value console.log(`or: ${value1 || value2 || check()}`); // true // && (and), finds the first fals value console.log(`and: ${value1 && value2 && check()}`); // false // often used to compress long if-statement // nullableObject && nullableObject.something if (nullableObject != null) { nullableObject.something; } function check() { for (let i = 0; i < 10; i++) { // wasting time console.log('kingsubin'); } } // ! (not) console.log(!value1); // false
7. Equality
const stringFive = '5'; const numberFive = 5; // == loose equality, with type conversion console.log(stringFive == numberFive); // true console.log(stringFive != numberFive); // false // === strict equality, no 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 console.log(ellie1 === ellie2); // false console.log(ellie1 === ellie3); // true // equality - puzzler console.log(0 == false); // true console.log(0 === false); // false console.log('' == false); // true console.log('' === false); // false console.log(null == undefined); // true console.log(null === undefined); // false
8. Conditional operators : if, else if, else
const name = 'ellie'; if (name === 'ellie') { console.log('Welcome, Ellie!'); } else if (name === 'coder') { console.log('You are amazing coder'); } else { console.log('unkwnon'); } // Welcome, Ellie!
9. Ternary operator : ?
console.log(name === 'ellie' ? 'yes' : 'no');
10. Switch statement
// use for multiple if checks // use for enum-like value check // use for muliple type checks in TS const browser = 'IE'; switch (browser) { case 'IE' : console.log('go away'); break; case 'Chrome' : case 'Firefox' : console.log('love u'); break; default: console.log('same all'); break; } // go away
11. Loops
// while loop, while the condition is truthy, // body code is executed. let i = 3; while (i > 0) { console.log(`while: ${i}`); i--; } // 3 2 1 // do while loop, body code is executed first, // then check the condition. do { console.log(`do while: ${i}`); i--; } while (i > 0); // 0 // for loop, for (begin; condition; step) for (i = 3; i > 0; i--) { console.log(`for: ${i}`); } // 3 2 1 for (let i = 3; i > 0; i = i - 2) { // inline variable declaration console.log(`inline variable for: ${i}`); } // 3 1 // 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 from 0 to 10 and print only even numbers (use continue) for (let i = 0; i < 11; i++) { if (i % 2 !== 0) { continue; } console.log(`q1. ${i}`); } // Q2. 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(`q2. ${i}`); }
※출처
'JavaScript & TypeScript' 카테고리의 다른 글
6. what is object (0) 2020.11.14 5. class vs object (0) 2020.11.14 4. Arrow Function (0) 2020.11.13 2. data types, let vs var, hoisting (0) 2020.11.13 1. script async vs defer (0) 2020.11.13