2021.01.12(화) TIL

HJ's Coding Journey·2021년 1월 12일
0

<조건문 (Conditional Operator)>

컴퓨터는 작업처리 능력이 뛰어나지만 인간과 다르게 의사소통을 효율적으로 하지 못한다. 컴퓨터가 우리 일상생활에 도움을 줄 수 있는 도구가 되려면, 인간과 비슷하게 작동할 수 있게 어떤 조건에 작동을 해야 한다는 것을 명시를 해줘야한다. 이런 갈등을 해결하기위해 우리는 조건문을 사용한다.

조건문을 알기위해서는 Boolean 타을 꼭 알아야한다. Boolean 을 사용하면 조건을 판별하는 기준을 정할 수 있다.

Ex)

let isAdult = true;
// 또는 false

let isStudent = false;
// 또는 true

비교의 결과는 늘 Boolean, 즉 true 혹은 false 다.

  • 비교연산자
    - '<' (More than)
    - '>' (Less than)
    - '<=' (More than or equal)
    - '>=' (Less than or equal)
    - '===' (Equal)
    - '!==' (Not equal)

'==' 와 '!=' 는 타입을 엄격하게 구분하지 않음으로 사용하지 않는다.

Ex)

3 > 5; // false
9 < 10; // true

조건문의 형식은 다음과 같이 작성한다:

if (조건1) {            // 조건1이 통과할 경우
} else if (조건2) {     // 조건1이 통과하지 않고, 조건2가 통과할 경우
} else {               // 모든 조건이 통과하지 않는 경우
}

위와같이 조건에는 Boolean 으로 결과가 나오는 표현식이 들어간다.

  • 논리연산자
    - 두가지 조건이 한번에 적용되는 경우에 사용할 수 있다.

Ex)

AND 연산자 (&&)

// 학생이면서, 여성일 때 통과
isStudent && isFemale;

NOT 연산자 (||)

// 학생이거나, 여성일 때 통과
isStudent || isFemale;

NOT 연산자 (!)

// 학생이 아니면서, 여성일 때 통과
!isStudent && isFemale;

<문자열 다루기 (String)>

  • str[index]
let str = 'CodeStates';
console.log(str[0]); // 'C'
  • concatenating strings
let str1 = 'Code';
let str2 = 'States';
let str3 = '1';
console.log(str1 + str2); // 'CodeStates'
console.log(str3 + 7); // '17'
  • str.length
let str = 'CodeStates';
console.log(str.length); // 10
  • str.indexOf
'Blue Whale'.indexOf('Blue); // 0
'Blue Whale'.indexOf('blue'); // -1
'Blue Whale'.indexOf('Whale'); // 5
'Blue Whale Whale'.indexOf('Whale'); // 5

'canal'.lastIndexOf('a'); // 3
  • str.split
let str = 'Hello from the other side';
console.log(str.split(''); // ['Hello', 'from', 'the', 'other', 'side']
  • str.substring (str.slice)
let str = 'abcdefghij';
console.log(str.substring(0, 3); // 'abc'
console.log(str.substring(3, 0); // 'abc'
console.log(str.substring(1, 4); // 'bcd'
console.log(str.substring(-1, 4); // 'abcd'
console.log(str.substring(0, 20); // 'abcdefghij'
  • str.toLowerCase() / str.toUpperCase()
console.log('ALPHABET'.toLowerCase()) // 'alphabet'
console.log('alphabet'.toUpperCase()) // 'ALPHABET'
profile
Improving Everyday

0개의 댓글