250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 자바스크립트 객체
- lie와 lay의 차이점
- VScode에서 들여쓰기 해제 하는 방법
- If
- boolean
- lie와 lay 비교
- lie 과거분사
- 왕초보 자바스크립트
- 자바스크립트 데이터타입
- python virtual environment create window
- lay 과거형
- 자바스크립트 for
- 지하온천에 대한 뉴스로하는 영어 공부
- 자바스크립트
- abc뉴스 영어공부
- 변수
- 자바스크립트 배열
- JavaScript
- VScode 주석 단축기 ctrl + / 안될때
- 대입연산자
- lay 과거분사
- DOM
- javascript for
- shift + tab
- Hot springs tourism
- lie 과거형
- Math.random
- python virtual environment create mac
- fizzbuzz
- 영어뉴스 영어공부
Archives
- Today
- Total
김숭늉 마음대로
자바스크립트 ===, ==, 논리연산자(logical operators) 본문
728x90
안녕하세요.
자바스크립트 = = = 와 = = 연산자의 차이와 자바스크립트 논리연산자 (logical operators)에 대해 알아보겠습니다.
자바스크립트 = = = 와 = = 연산자의 차이?
자바스크립트의 === 와 == 연산자의 차지는 ===은 strict해서 값과 두 변수의 유형 모두 체크하지만 ==의 경우는 값만 비교하여 체크한다. 실제 개발을 하면서는 항상 ===의 등호를 이용하는것이 좋다 ! 버그를 피하기 위해서는!
# 간단한 예제
const age = 18;
if (age === 18) console.log("You just became an adult (strict) :)"); // 조건이 하나밖에 없으면 {} 안써도 됨! 한줄이 작성해도 무방함!
if (age == 18) console.log("You just became an adult (loose) :)"); // 조건이 하나밖에 없으면 {} 안써도 됨! 한줄이 작성해도 무방함!
* 위의 경우 모두 true의 값을 반환한다
const favourite = Number(prompt("What's your favourite number?")); // prompt에 작성된 값을 저장함
console.log(favourite); // prompt에 입력한 텍스트가 나옴
console.log(typeof favourite); // string
if (favourite == 23) {
// '23' == 23
console.log("cool 23 is amazing number!"); // 정상 출력됨 왜냐하면 ==을 사용했기 때문에
}
* 위의 경우 문자열인 '23'과 23은 ==로 비교하는경우 true의 값을 반환한다.
if (favourite === 23) {
// '23' === 23
console.log("cool 23 is amazing number!"); // 출력되지 않음 왜냐하면 === 을 사용했기 때문에
}
* 위의 경우 문자열인 '23'과 23은 ===로 비교하는경우 false의 값을 반환한다. (type이 맞지 않기 때문에)
자바스크립트 논리연산자 (logical operators)
자바스크립트의 논리 연산자는 ||(OR), &&(AND), !(NOT)가 있습니다.
||(OR) A와 B 둘중 하나만 참이거나 둘다 참인경우 true
&&(AND) A와 B 둘다 모두 참인 경우에만 true
!(NOT) true면 false로 변경, false면 true로 변경 합니다.
const hasDriversLicense = true; // A
const hasGoodVision = false; // B
console.log(hasDriversLicense && hasGoodVision); // false
console.log(hasDriversLicense || hasGoodVision); // true
console.log(!hasDriversLicense); // false
const shouldDrive = hasDriversLicense && hasGoodVision; // true && false = false
const isTired = true; // C
console.log(hasDriversLicense || hasGoodVision || isTired); // true or false or true === true
if (hasDriversLicense && hasGoodVision && !isTired) {
// false
console.log("sarah is able to drive");
} else {
console.log("someone else should drive the car"); // 콘솔창 출력됨
}
const scoreDolphins = (96 + 108 + 89) / 3;
const scoreKoalas = (88 + 91 + 110) / 3;
console.log(scoreDolphins, scoreKoalas);
if (scoreDolphins > scoreKoalas) {
console.log("Dolphins win the trophy");
} else if (scoreKoalas > scoreDolphins) {
console.log("Koalas win the trophy");
} else {
console.log("Both win the trophy");
}
그럼 오늘도 좋은하루 보내세요.🤍
728x90
반응형
'IT > JavaScript' 카테고리의 다른 글
자바스크립트 스트릭트 모드 (Javascript strict mode) (0) | 2023.09.12 |
---|---|
스위치 조건문(switch statement), 삼항 연산자 The Conditional (Ternary) Operator (0) | 2023.09.11 |
자바스크립트 타입 변환 (type conversion), 강제 형변환 (type coercion), 그리고 불리언(boolean) (0) | 2023.09.06 |
자바스크립트, javascript if else, control structure(컨트롤 스트럭쳐) (0) | 2023.09.06 |
자바스크립트에서 백틱 사용, template literal, javascript usage of backticks (0) | 2023.09.06 |