JS 21.03.03 (if, string method, Math, Hoisting, template literal)


  • 중첩 if 문
if(조건1){
    if(조건2){
        if(조건3){
            실행3;
        }
    }
}

// 조건1,2,3이 true면 실행3
  • 중첩 if-else 문
if(조건1){
    if(조건2) {
        실행2;  
    }
    실행1
} else if(조건3){
    실행3;
} else 실행4;

// 조건1,2가  true면 실행2
// 조건1이 true고 조건 2가 false면 실행1
// 조건1이 false고 조건3이 true면 실행3
// 조건1,2가 false면 실행 3
  • if vs else if
if(조건1){
    실행1;
}
if(조건2){
    실행2;
}
if(조건3){
    실행3;
}

// 조건1을 true면 실행1
// 조건2를 true면 실행2
// 조건3를 true면 실행2
// 조건 1,2,3이 전부 실행 될 수 있다.

if(조건1){
    실행1;
} else if(조건2){
    실행2;
} else 실행3;
}
// 조건1이 true면 조건 1을 실행하고
// 조건1이 false고 조건2가 true면 실행2
// 조건 1,2가 false면 실행3
  • if 문에서 false로 변환되어 if 구문이 실행되지 않는 조건
if(조건이 false인 경우)
if(false)  
if(null) 
if(undefined) 
if(0) 
if(NaN)
if('')
string method
  • 모든 string method는 immutable, 즉 원본이 변하지 않음. (array method는 원본을 변경하는 method도 존재)
let str = 'creamer';
  • 문자열 접근: charAt(), [index]
str.charAt(1) // r
str[1] // r
// (index는 0부터 시작하기 때문에)

str[0] = 'd';
// 'd'
console.log(str) 
//creamer
// string에 접근은 가능하지만 string 자체를 수정하지는 않는다. 
// 즉, 읽기 전용 + (일부 array method는 array를 변경한다.)
  • includes(검색어) : string에서 검색어가 있는지 체크하고 boolean 값으로 return
str.includes('d') // false
str.includes('c') // true
  • indexOf(검색어) : string에서 해당 검색어가 맨 처음 시작하는 index를 return
  • lastIndexOf(검색어) : 역순으로 string에서 해당 검색어가 맨 처음 시작하는 index를 return
str.indexOf('d') // -1 (검색어가 없는 경우)
str.indexOf('c') // 0
str.indexOf('e') // 2 (검색어가 2개 있는 첫번째 index를 return)

str.lastIndexOf('d') // -1 (검색어가 없는 경우)
str.lastIndexOf('c') // 0
str.lastIndexOf('e') // 5 (검색어가 2개 있는 역순에서 첫번째 index를 return)
  • repeat(반복할 횟수) : 반복할 횟수 만큼 반복. 반복 횟수는 양의 정수
str.repeat(3);
// 'creamercreamercreamer'
  • replace(바꿀 문자, 새로운 문자) : 바꿀 문자를 새로운 문자로 치환
  • replaceAll(바꿀 문자, 새로운 문자) : 모든 바꿀 문자를 새로운 문자로 치환
str.replace('r', 'k');
// 'ckeamer'
str.replaceAll('r', 'k');
// "ckeamek"
str
// 'creamer'
  • slice(startIndex, endIndex) : string의 일부를 추출
  • substring(startIndex[, endIndex]) : startIndex 부터 endIndex 전 까지 string의 일부를 추출
  • string.substr(startIndex, length) : startIndex부터 length 길이만큼 string을 잘라내어 반환하 차이점 상세 사항 : https://hianna.tistory.com/340
str.slice(1,3);
// re
str.substring(1,3);
// re
str.substr(1,3);
// rea
  • split(separator) : separator를 기준으로 나눠 array 생성.
str = 'cream+er'
str.split('+')
(2) ["cream", "er"]
  • String.toLowerCase() : 대문자로 변환
  • String.toUpperCase() : 소문자로 변환
let str = 'CREAMer';
str.toUpperCase();
// "CREAMER"
str.toLowerCase();
// "creamer"
  • String.trim() : 양 끝의 공백을 제거
  • String.trimEnd() : 맨 끝의 공백을 제거
  • String.trimStart() : 맨 앞의 공백을 제거
str = '     creamer      '
str.trim();
// 'creamer'
str.trimEnd();
// '     creamer'
str.trimStart();
// 'creamer      '
  • +string 연산자 : string 타입과 다른 타입 사이에 + 연산자를 사용하면 string 형식으로 변환
'creamer' + 7 = 'creamer7'
Math
  • Math.abs(number) : number의 절댓값을 반환
Math.abs(-100) // 100
  • Math.ceil(number) : 올림
Math.ceil(9.5) // 10
Math.ceil(-9.5) // -9
  • Math.floor(number) : 내림
Math.floor(9.5) // 9
Math.floor(-9.5) // -10
  • Math.round(number) : 반올림
Math.round(9.5);
10
Math.round(9.4);
9
  • Math.max(x, y, z …) : 0개 이상의 인수에서 제일 큰 수를 반환
Math.max(1, 2, 3) // 3
  • Math.pow(x, y) : x의 y제곱
Math.pow(2, 3) // 8
  • Math.random() : 0과 1 사이의 난수를 반환
Math.random() // 0 < random한 수 < 1
  • Math.sign(numver) :
Math.sign(100) // 양수면 1 
Math.sign(-100) // 음수면 -1
Math.sign(0) // 0
  • Math.trunc(number) : 숫자의 정수 부분을 반환
Math.trunc(10.5) // 10
Math.trunc(-10.5) // 10
Hoisting

var를 사용하면 변수의 호이스팅 문제가 생기므로 let과 const 사용

호이스팅이란 : https://developer.mozilla.org/ko/docs/Glossary/Hoisting

template literal (template string)
  • literal : 변수에 넣는 변하지 않는 data를 의미

  • const : 변하지 않는 변수

// backtick(`) 사용
`${변수} ${인자} 띄어쓰기와 operator를 별도로 따옴표에 넣어줄 필요 없음`





© 2020.11. by creamer

Powered by CREAMer