JavaScript Conditional Statements

김서하·2021년 4월 5일
0

JavaScript

목록 보기
3/11
*If문*
 if (true) {
  console.log('This message will print!');
 } // Prints: This message will print!
 -> if 다음 () 안에 true 나 false 하고 {}로 
    조건이 true로 평가되면 {}안의 코드가 실행
    false이면 실행되지 않는다
    
 *If...Else문*
  if (false) {
    console.log('The code in this block will not 
  run.');
  } else {
    console.log('But the code in this block     
  will!');
  }    
  // Prints: But the code in this block will!
  -> if 문이 false로 결정나면 else 코드가 실행됨
  
 *비교 연산자*
  보다 작음      : <
  보다 큼        : >
  보다 작거나 같음 : <=
  보다 크거나 같음 : >=
  동일하다       : ===
  같지 않음      : !==
  
  왼쪽 값과 오른쪽 값을 비교하고 맞으면 true, 아니면 false
  
*논리 연산자*
 and : &&
 or  : ||
 not : !
 ex) if (stopLight === 'green' && 
     pedestrians === 0) {
       console.log('Go!');
     } else {
       console.log('Stop!');
     } // 조건 두 가지 모두 true일 때 Go! 출력,
          하나라도 false일 때 Stop!
           
     if (day === 'Saturday' || day === 'Sunday') 
     {
       console.log('Enjoy the weekend');
     } else {
       console.log('Do some work');
     } // 조건 두 가지 중 하나라도 맞으면 
          Enjoy the weekend
           
     let excited = true;
     console.log(!excited); // Prints false
     
 *True와 False*
  false 값 목록
  0
  "" 또는 ''
  null(값이 없다)
  undefined
  NaN(Not a Number) 로 하면 false값 출력됨
  
 ex) let tool = ''
     let writingUtensil = tool || 'pen';
     console.log(`The ${writingUtensil} is
     mighter than the sword.`);
     // Prints The pen is mighter than the sword.
     
     let tool = 'marker'
     let writingUtensil = tool || 'pen';
     console.log(`The ${writingUtensil} is
     mighter than the sword.`);
     // Prints The marker is mighter than the 
        sword.
        
*삼항 연산자*
 if...else문 간략히
 
 let isNightTime = true;
     
 if(isNightTime) {
    console.log('Turn on the lights!');
  } else {
    console.log('Turn off the lights!');
  }
  를 
  isNightTime ? console.log('Turn on the 
  lights!') : console.log('Turn off the lights!); 
  로
  ->> 조건 ? 뒤에는 : 로 구분
      true 이면 첫번째 표현식 false 이면 두번째 표현식
      
 *Else If문*
  항상 if 문 뒤, else 문 앞에, 그 사이에 자리함!
  
  let stopLight = 'yellow';
 
  if (stopLight === 'red') {
    console.log('Stop!');
  } else if (stopLight === 'yellow') {
    console.log('Slow down.');
  } else if (stopLight === 'green') {
    console.log('Go!');
  } else {
    console.log('Caution, unknown!');
  } 
  // Prints Slow down.
  
 *Switch문*
  else if 문 간략히
  
  let groceryItem = 'papaya';
 
  switch (groceryItem) {
    case 'tomato':
      console.log('Tomatoes are $0.49');
      break;
    case 'lime':
      console.log('Limes are $1.49');
      break;
    case 'papaya':
      console.log('Papayas are $1.29');
      break;
    default:
      console.log('Invalid item');
      break;
   }
   // Prints 'Papayas are $1.29'
      case
      값이 true면 console.log 실행
      
      break
      는 반복문 종료시킴 case 값이 true면 
      console.log 출력하고 빠져나와 종료 밑의 코드 확인x
      
      default
      는 case 모두 true아니면 
      default의 코드가 실행
      
      
profile
개발자 지망생 서하입니당

0개의 댓글