TIL JavaScript from Dream Coding(2)

doradora523·2022년 5월 26일
0

DreamCoding

목록 보기
2/9
post-thumbnail

1. String concatenation

console.log('my'+' cat'); // my cat
console.log('1'+ 2); // 12
console.log(`string literals: 1 + 2 = ${1 + 2}`); // string literals: 1 + 2 = 3
console.log('amanda\s \n\tbook') 
// amanda's
//   book
// \ : ' , \n : 세로줄바꿈, \t : tab

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;
// counter = counter + 1;
// preIncrement = counter;
console.log(`preIncrement: ${preIncrement}, counter: ${counter}`);
// preIncrement: 3, counter: 3

const postIncrement = counter++;
// postIncrement = counter;
// counter = counter + 1;
console.log(`postIncrement: ${postIncrement}, counter: ${counter}`);
// preIncrement: 3, counter: 4

const preDecrement = --counter;
console.log(`preDecrement: ${preDecrement}, counter: ${counter}`);
// preDecrement: 3, counter: 3

const postDecrement = counter--;
console.log(`postDecrement: ${postDecrement}, counter: ${counter}`);
// preIncrement: 3, counter: 2

4. Assignment operators

let x = 3;
let y = 6;
x += y; // x = x + y;
x -= y; // x = x - y;
x *= y; // x = x * y;
x /= y; // x = x / y;

5. Comparison operators

console.log(10 < 6); // less than // false
console.log(10 <= 6); // less the or equal // false
console.log(10 > 6); // greater than // true
console.log(10 >= 6) // greater than or equal  // true

6. Logical operators: || (or), && (and), ! (not)

const value1 = false;
const value2 = 4 < 2;

// || (or), finds the first truthy value
// 첫번째로 true를 찾으면 나머지 조건은 계산하지 않음
// 연산을 많이하는 함수를 호출하거나 expration 같은 것들은 제일 마지막 조건으로 둔다
console.log(`or: ${value1 || value2 || check()}`); // or: true

// && (and), finds the first falsy value
// 첫번째로 false를 찾으면 나머지 조건은 계산하지 않음
console.log(`and: ${value1 || value2 || check()}`); // and : false

function check() {
  for (let i = 0; i < 10; i++) {
    //wasting time
    console.log('😍')
  }
  return true;
}
// often used to compress long if-statement
nullableObject && unllableObject.somthing
if (nullableObject != null) {
  nullableObject.somthing;
}

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

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
consoel.log(0 === false); // false
consoel.log('' == false); // true
consoel.log('' === false); // false
consoel.log(null == undefined); // true
consoel.log(null === undefined); // false

8. Conditional operator : if

if, else if, else

const name = 'df';
if (name === 'amadna') {
  console.log('Welcome, Amadna!');
} else if (name === 'coder') {
  console.log('You are amazing coder');
} else {
  console.log('unknown');
}

// unknown

9. Ternary operator : ?

condition ? value1 : value2;

console.log(name === 'amadna' ? 'yes' : 'no'); // no

10. Switch statement

use for multiple if checks
use for enum-like value check
use for multiple type checks in TS

const browser = 'IE';
switch (browser) {
  case 'IE':
    console.log('go away!');
    break;
  case 'Chrome':
  case 'FireFox':
    console.log('I love You');
    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--;
}
// while: 3
// while: 2
// while: 1

do while loop, body code is excuted first,
then check the condition

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

for loop, for(begin; condition; step)

for (i = 3; i > 0; i++) {
  console.log(`for: ${i}`);
}

for (let i = 3; i > 0; i = i - 2) {
  // inline variable declaration
  console.log(`inline 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 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}`);
}

for (let i = 0; i < 11; i++) {
  if (i % 2 === 0) {
    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}`);
}

DreamCoding Lectures

0개의 댓글