JavaScript #12 ~ Boolean Logic & Logical Operators

HJ's Coding Journey·2021년 6월 9일
0

[Learn] JavaScript

목록 보기
13/20

Boolean Logic

Boolean logic is a branch of computer science that uses true or false values to solve complex logical problems. This concept is not only introduced in JavaScript, but also in many other programming languages. In this post, I want to talk about another set of operators that can be used with true and false values. The operators are as follows:

AND Operator (&&) : only 'true' when BOTH conditions are true

// This person is a female student 
isStudent && isFemale;

OR Operator (||) : only true when at least EITHER one of the conditions are true

// this person is either a female or a student
isStudent || isFemale;

NOT Operator (!) : when condition is true and the other condition is false (inverts true or false values)

// this person is a female, but not a student
!isStudent && isFemale;

Logical Operators

We can use the concept of boolean logic with logical operators to make decisions for complex problems. Following the AND, OR, and NOT operators, results will vary depending on the chosen logical route.

// 2 true
const isMale = true;
const isAdult = true;
// ---
console.log(isMale && isAdult); // true
console.log(isMale || isAdult); // true
console.log(!isMale); // false
// 1 true, 1 false
const isMale = true;
const isAdult = false;
// ---
console.log(isMale && isAdult); // false
console.log(isMale || isAdult); // true
console.log(!isMale); // false
// 2 false
const isMale = false;
const isAdult = false;
// ---
console.log(isMale && isAdult); // false
console.log(isMale || isAdult); // false
console.log(!isMale); // true
profile
Improving Everyday

0개의 댓글