한 걸음씩
[JS][udemy] JS 판단 내리기 본문
1. Boolean Logic
► Comparisons
- Notice these all return a Boolean
- You can compare strings
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
== | equality |
!= | not equal |
=== | strict equality |
!=== | strict non-equality |
► Double equals
- 비교하는 타입이 다를 경우 강제로 타입을 맞춰서 비교하기 때문에 원하는 값이 나오지 않을 수 있다
1 == '1'
>> true
► Triple equals
- 타입을 구분하고 값과 타입 모두 비교함
- 값을 비교할 때는 항상 삼중 등호를 사용할 것
1 === '1'
>> false
2. console, alert, prompt
- 메서드 종류 중 하나
► console.log
- python의 print와 동일
- console.log를 사용하지 않는다면 읽고 쓰기만 가능
- 출력을 위해서는 반드시 console.log 사용할 것
► alert
- 사용자에게 뭔가를 출력해 주지만 콘솔에서 출력하지는 않음
- 웹 페이지에 팝업 경고 창 나타남
► prompt
- 인수를 받아들임
- 웹 페이지에 입력 팝업 창 나타남
let userInput = prompt("please enter a number")
// 웹 페이지에 팝업 입력창이 뜨고 '97'(임의의 숫자)을 입력
userInput
>> '97' // 타입은 문자
userInput + 1
>> '971'
parseInt(userInput) // 정수로 변환
>> 97
parseInt('101asdfsd')
>> 101 // 정수만 찾아서 반환
parseInt(userInput) + 1
>> 98
https://developer.mozilla.org/ko/docs/Web/API/Console
console - Web API | MDN
console 객체는 브라우저의 디버깅 콘솔(Firefox 웹 콘솔 등)에 접근할 수 있는 메서드를 제공합니다. 동작 방식은 브라우저마다 다르지만, 사실상 표준으로 여겨지는 기능도 여럿 있습니다.
developer.mozilla.org
https://developer.mozilla.org/ko/docs/Web/API/Window/alert
Window.alert() - Web API | MDN
Window.alert() 메서드는 확인 버튼을 가지며 메시지를 지정할 수 있는 경고 대화 상자를 띄웁니다.
developer.mozilla.org
https://developer.mozilla.org/ko/docs/Web/API/Window/prompt
Window.prompt() - Web API | MDN
Window.prompt()는 사용자가 텍스트를 입력할 수 있도록 안내하는 선택적 메세지를 갖고 있는 대화 상자를 띄웁니다.
developer.mozilla.org
3. JS 실행하기
4. Conditionals
- python if / elif / else와 사용법 같음
► IF STATEMENT
const rating = 3;
if (rating === 3) {
console.log("YOU ARE A SUPERSTAR!");
}
// if문이 true인 경우 "YOU ARE A SUPERSTAR!" 문구를 출력
► ELSE IF
- if가 false일 경우 else if 실행
const dayOfWeek = "monday";
if (dayOfWeek === "monday") {
console.log("I hate Mondays");
} else if (dayOfWeek === "Saturday"){
console.log("I love saturdays");
} else if (dayOfWeek === "Friday"){
console.log("I like Fridays");
}
► ELSE
- if, else if 둘 다 false일 경우 else 실행
const dayOfWeek = prompt("Enter a day").toLowerCase();
if (dayOfWeek === "monday") {
console.log("I hate mondays");
} else if (dayOfWeek === "saturday"){
console.log("I love saturdays");
} else if (dayOfWeek === "friday"){
console.log("I like fridays");
} else {
console.log("keep going");
}
5. Nesting
const password = prompt('please enter a new password');
// Nesting 조건안에 조건을 넣어서 판별
if (password.length >= 6) {
if (password.indexOf(' ') === -1) { // 입력받은 비밀번호가 공백이 아니라서 -1일 경우
console.log('valid password!) // 다음과 같이 출력
} else {
console.log('password cannot contain spaces!') // 공백일 경우
}
} else { // 위의 중첩 조건문을 만족하지 못했을 경우 실행
console.log('password too short! must be 6+ characters')
}
6. Truth-y and False-y
► All Js calues have an inherent truthyness or falsyness about them
► Everything esle is truthy
► Falsey values
- false
- 0
- ""(empty string)
- null
- undefined
- NaN
7. Logical operators
- python 논리 함수와 동일
► AND : &&
true && true
>> true
false && false
>> false
true && false
>> false
► OR
- If one side is true, the entire thing is true
1 !== 1 || 10 === 10
>> true
10/2 === 5 || null
>> true
0 || undefined
>> false
► NOT
- !expression returns true if expression is false
!null
>> true
!(0 === 0)
>> false
!(3 <= 4)
>> false
const age = 8;
if (!(age >= 0 && age < 5 || age >= 65)) {
// 나이가 0 이상 5 미만이거나 65 이상이 아닌 경우에 다음을 출력해라
console.log("You are not a baby or a senior!")
}
>> You are not a baby or a senior!
8. Switch 조건문
const day = 2;
switch(day) {
case 1:
console.log("MONDAY");
break;
case 2:
console.log("TUESDAY");
break;
case 3:
console.log("WENDESDAY");
break;
case 4:
console.log("THURSDAY");
break;
case 5:
console.log("FRIDAY");
break;
default: // 위의 조건문이 전부 false일 경우 다음과 같이 출력
console.log("I DONT KNOW THAT")
}
>> TUESDAY
- break를 하지 않으면 일치하는 구문 이후로 계속 출력됨
- 자주 사용하는 조건문은 아님
'JS' 카테고리의 다른 글
[JS][udemy][multicampus] JS Object Literals (0) | 2023.03.14 |
---|---|
[JS][multicampus]Basic syntax of JavaScript (0) | 2023.03.14 |
[JS][udemy][multicampus] Arrays (0) | 2023.03.13 |
[JS][multicampus] History and DOM (0) | 2023.03.13 |
[JS][udemy] Primitive Types (0) | 2023.03.08 |