JS #2 데이터 타입, 변수, 태그 선택, 조건문, 리팩토링
- properties
문자열.length
문자열의 크기.
- method
문자열.toUpperCase()
문자열을 모두 대문자로.
문자열.indexOf('o')
o 문자가 있는 위치 반환.
문자열.trim()
문자열 내 공백을 없애준다.
"1" + "1"
"11"이 된다.
변수명 앞에 var 적는 습관 중요.
document.querySelector('#target')
id가 target인 태그를 선택.
조건문은
if(..===..){
} else{
}
'===' 비교 연산자.
<input id="night_day" type="button" value="night">
..
document.querySelector('#night_day').value
-> id가 night_day인 태그의 value 값 반환, 여기서는 night.
리팩토링.
중복 제거 통해 관리 수월하게.
중복이 발생하면 무조건 제거하라.
document.querySelector('#night_day').value
를
this.value
로 바꾸고.
<input id="night_day" type="button" value="night">
를
<input type="button" value="night">
로 바꾼다.
this는 현재 속한 태그를 가리킨다. (id 등 필요 X)
또한
document.querySelector('body').style....
위 문장이 반복해서 나오면 그 위에
var target = document.querySelector('body');
을 작성해서
document.querySelector('body').style....
를
target.style..
로 줄일 수 있다.