Conditional(조건문)
const age = parseInt(prompt("How old are you?"));
console.log(isNaN(age));

prompt(massage)
사용자에게 input을 받을 수 있는 창을 띄움
message와 입력값은 string으로 받고 저장
오래된 기술
typeof 변수
변수의 type을 확인
parseInt(변수)
숫자로 이루어진 string 변수를 number(int)로 바꿔줌
문자 string은 NaN(Not a Number)로 표시됨(변수가 숫자인지 아닌지 확인 가능)
isNaN(변수)
NaN인지 아닌지 확인
return : boolean (true or false)
if
if(condition){
// condition이 true일 경우 실행
} else {
// condition이 false일 경우 실행
}
condition에는 boolean이 들어감 (true or false)
else는 생략 가능
else if로 조건을 추가
const age = parseInt(prompt("How old are you?"));
if(isNaN(age) || age < 0){
console.log("Please write a real positive number");
} else if(age < 18) {
console.log("You are too young.");
} else if(age >= 18 && age <= 50) {
console.log("You can drink");
} else if(age > 50 && age <= 80) {
console.log("You should exercise");
} else if(age === 100) {
console.log("wow you are wise");
} else if(age > 80) {
console.log("You can do whatever you want.");
}
* if문 순서 중요
if (age > 80)과 if (age===100) 순서가 바뀌면 후자는 적용이 안됨
age > 80 조건을 실행하고 끝남
연산자
| || | OR (하나가 참이면 참) |
| && | AND (모두 참이여야 참) |
| ===/!== | 같음/같지 않다(변수의 타입까지 고려) 예) 0 === false ---> false |
| ==/!= | 같다/같지 않다(값만 비교) 예) 0 == false ---> true |
'개발 > JavaScript' 카테고리의 다른 글
| 바닐라 JS - Events (0) | 2021.08.25 |
|---|---|
| 바닐라 JS - document object (0) | 2021.08.23 |
| 바닐라 JS - function (0) | 2021.08.05 |
| 바닐라 JS - Array, Object (0) | 2021.08.03 |
| 바닐라 JS - Data type, Variable (0) | 2021.07.30 |