TIL (2020.07.19)

Awesome·2020년 7월 19일
0

TIL

목록 보기
17/46

Javascript 기초

아 하기 싫지만.. 배워야 하니까 다시 기초부터 가보자.

Conditional Statements

자바스크립트는 조건문을 표현하는 방식이 매우 여러개다.

1) 기본 구문

let sale = false;

if (sale) {
  console.log("Time to buy!")
} else {
  console.log("Do not buy!")
};

2) Truthy and Falsy Assignment

let tool = '';

// Use short circuit evaluation to assign  writingUtensil variable below:
// 변수 tool 이 정의되어 있으면 그 값을 쓰고, 그렇지 않으면 'pen' 으로 정의됨
let writingUtensil = tool || "pen";

console.log(`The ${writingUtensil} is mightier than the sword.`);

3) Ternary Operator

let isLocked = false;

isLocked ? console.log('You will need a key to open the door.') : 
console.log('You will not need a key to open the door.');
  • 조건이 true 이면 ? 뒤의 실행문

  • 조건이 false 이면 : 뒤의 실행문

    4) The switch keyword

let athleteFinalPosition = 'first place';

switch (athleteFinalPosition) {
  case "first place" :
    console.log("You get the gold medal!");
    break;
  case "second place" :
    console.log("You get the silver medal!");
    break;
  case "third place":
    console.log("You get the bronze medal!");
    break;
  default :
    console.log("No medal awarded.");
    break;
}
  • 조건이 해당하는 case 만 실행되며, break 를 통해 바로 종료됨
profile
keep calm and carry on

0개의 댓글