본문 바로가기
Programming/JavaScript

[JavaScript] Conditional Statements (조건문)

by inyeong 2024. 9. 25.

1. If statement 

( ) 안에 true or false로 판단할 수 있는 조건 

 

조건이 true이면 { } 내부 문장 실행
 

if (true) {
  console.log('This message will print!'); 
}
// Output : This message will print!

 

2. If ... else statement 

조건이 false이면 else 내부 문장 실행

if (false) {
	console.log('The code in this block will not run.');
} else {
	console.log('But the code in this block will!');
}
// Output : But the code in this block will!

 

3. Comparison Operators (비교 연산자)

<  >  <=  >=  ===  !==

ex)
10 < 12 // true 
'apples' === 'oranges'
// false


4. Logical Operators (논리 연산자)

&& (and), || (or),  ! (not)

if (stopLight === 'green' && pedestrians === 0) {
  console.log('Go!');
} else {
  console.log('Stop');
}

if (day === 'Saturday' || day === 'Sunday') {
  console.log('Enjoy the weekend!');
} else {
  console.log('Do some work.');
}

let excited = true;
console.log(!excited); // Prints false

let sleepy = false;
console.log(!sleepy); // Prints true

 

5. truthy and falsy

string이나 number 자료형은 값이 할당되었는지에 따라 true / false가 결정된다. 

falsy values : 0   " "   ' '   null   undefined   NaN

let myVariable = 'I Exist!'; // truthy value, non-falsy value 

if (myVariable) {
   console.log(myVariable)
} else {
   console.log('The variable does not exist.')
   
   
let numberOfApples = 0; // falsy value 

if (numberOfApples){
   console.log('Let us eat apples!');
} else {
   console.log('No apples left!');
}

// Prints 'No apples left!'

 

6. Ternary Operator

if문 단순화 

조건 ? true일 때 실행할 문장 : false일 때 실행할 문장 

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!');

 

7. else if statement 

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!');
}

 

8. switch

let groceryItem = 'papaya';

if (groceryItem === 'tomato') {
  console.log('Tomatoes are $0.49');
} else if (groceryItem === 'papaya'){
  console.log('Papayas are $1.29');
} else {
  console.log('Invalid item');
}


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'

 


 

예제 코드 : https://github.com/inyeongjang/Snoopy/blob/main/week02/2-Conditional.js

 

Snoopy/week02/2-Conditional.js at main · inyeongjang/Snoopy

Contribute to inyeongjang/Snoopy development by creating an account on GitHub.

github.com

 

참고자료https://www.codecademy.com/enrolled/courses/introduction-to-javascript

 

Learn JavaScript | Codecademy

Begin your web development journey with our JavaScript course. Explore dynamic scripting for interactive web solutions.

www.codecademy.com

 

 

'Programming > JavaScript' 카테고리의 다른 글

[JavaScript] Loops & Iterators  (0) 2024.09.28
[JavaScript] Arrays (배열)  (0) 2024.09.25
[JavaScript] Scope  (0) 2024.09.25
[JavaScript] Functions (함수)  (0) 2024.09.25
[JavaScript] JavaScript 기초  (0) 2024.09.23