JavaScript Boolean Type

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Professional Modern JavaScript Lessons

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36

JavaScript Boolean Type

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Values

const Yes = true;
const No = false;

typeof

console.log(typeof Yes); // 'boolean'
console.log(typeof No); // 'boolean'

Result of Equality

let person = 'John';
console.log(person === 'John'); // true
console.log(person === 'Jane'); // false

Conditional Blocks

let person = 'Judy';

if (person === 'Judy') {
console.log('Hi J!');
}

if (person === 'Kate') {
console.log('Hey K!');
}

Bang Bang

Used for boolean conversion

console.log(!true); // false
console.log(!false); // true

console.log(!!true); // true
console.log(!!false); // false

console.log(!!''); // false
console.log(!!'Hello World'); // true

And Or Combinations

||
let state = 'Pending';

if (state === 'Pending') {
console.log('Valid');
} else if (state === 'Done') {
console.log('Valid');
} else {
console.log('Unexpected state!');
}

if (state === 'Pending' || state === 'Done') {
console.log('Valid');
} else {
console.log('Unexpected status!');
}
&&
/** @type {number} Range: 100 - 599 */
let httpCode = 200;

if (httpCode === 200) {
console.log('Success');
} else if (httpCode >= 400 && httpCode <= 500) {
console.log('Error!');
} else {
console.log('Unexpected status!');
}

Naming Conventions

/** Will be true if user is logged in */
let userLoggedIn; // ❌
let isUserLoggedIn; // ✅

/** Will be true if user has access */
let access; // ❌
let hasAccess; // ✅

/** Will be true if user can dance */
let dance; // ❌
let canDance; // ✅

/** Will be true if user should provide their address */
let provideAddress; // ❌
let shouldProvideAddress; // ✅

Avoid Negative Naming

let isNotAllowed; // 😡
isNotAllowed = true;
isNotAllowed = false; // 😡

let isAllowed; // 🤗
isAllowed = false;
isAllowed = true; // 🤗
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

Perhaps the similar stenotype in all of JavaScript is the humble bullion, but nonetheless, it is critical to writing useful programs. So let's take a look. A bullion is a general programming concept and it always has two values and within JavaScript, these values are represented by the literals, lowercase two and lowercase. False. Using the JavaScript type of operator on a bullion type gives back the string bullion, which is of course why we are calling them bullion. We don't normally use a bullion by itself as it often shows up as a result of a comparison operator. For example, hey, we have a variable called person,

00:34

which we have initialized to John, and if we apply the triple equals operator, which is called a strict equality within JavaScript between the person and the string, John, we get back through. Similarly, if we compare it to something else, for example, the string chain, we get back the Boolean false. The key use case for a Boolean is when it shows up as a result in conditional logic. For example, hey we have a person that might be Judy or it might be Kate. If person is going to be Judy, we are going to log to the console Hi J, and if it's going to be Kate, we are going to log, Hey Kate,

01:08

right now we have initialized person to Judy. This means that the triple equal comparison with the string Judy will be true. And similarly, the triple equal comparison with gait will result in false. The way the if condition works within JavaScript is if the expression provided to if is toothy and of course true is a toothy value, then the block of code contained within if will execute. And similarly, if the expression evaluates to a falsey value and false is a falsey value, then the block of code will be skipped over and if we execute this code, no surprise we see Hi J.

01:41

Now if you were to modify the person variable to be Kate and then execute the code again, of course this time we see hey K in terms of bullion operators, one that we really need to care about is the exclamation point which is lovingly called the bank and when used twice it is called the double bank. The exclamation point is the negation operator and it is also called the logical knot. And the logical compliment its behavior is quite simple. It takes it true and turns it into a false and it takes a false and turns it into a true and you can actually use it twice and for a bullion it would have no side effect. So the two will become false and then will become true again.

02:19

And similarly false will become true and then false back again. But this is great for converting falsely or truthy values of other data types into true bullion. True and false and empty string within JavaScript is a falsey. So applying double bang to it converts it into a true Boolean false. And similarly, a non empty string is actually truthy, so applying double bang to that converts it to a Boolean. True using truthy and Falsey values within conditions is normally frowned upon and you really want to use real bullion, true and false and using the double bang allows you to convert these into real

02:53

bullions. We can combine to bullion expressions with the operators double and percent For and and double pipe for. Or here we have a variable defining a state that can be pending or done and because of some unforeseen circumstances it might end up being something else, in which case we want to show an error. So if it is indeed pending, we show valid. If it is done, we show valid. Otherwise we show unexpected state notice a duplication of code between the pending and the done code blocks. Wouldn't it be great if you could combine these both conditions into a single

03:28

conditional and that is where the OR operator comes into play. If either of these conditions state equal to pending or state equal to done are true, then the whole of the condition will turn into true. That is how the boo or operator works and it allows us to combine those two conditions into a single conditional block so we don't have to repeat ourselves again and again. The opposite of the boo or is the BOO and operator. And unlike or where either of the conditions being true results in the whole thing be true with the Boolean and all of the conditions have to be true for the final expression to result to true.

04:02

Here we have a variable for an S T T P status code, which can be between one hundred and five nine nine. If it is 200, it is success, but if it is in the range 400 to 500, we want to show an error and to combine the conditions that it must be greater than equal to 400 and less than equal to 500. We use the Boolean end. So as an example, right now it is 200, so it'll fall down to success, but if it is four, four not found, it is going to lie in the range 400 and 500 and therefore we will log out error and for any other value. For example, 3 0 1 moved permanently, it is not 200,

04:39

it is not in the range 400 to 500 and therefore we log out unexpected status when you have a variable that is intended to store a bullion value, some developers have a very strong feelings about what these variables should be named and specifically how they should be prefixed. Here are a few examples to give you an intuition about the most common prefixes. If you want a variable that will be true if the user is logged in. Don't call it user logged in because it's confusing. If this is a bullion value or an object containing some details about the user that is logged in instead,

05:12

prefer the more obviously Boolean looking is user logged in. Similarly, if you have a bullion for when the user has access, don't call it access because it looks more like an object and instead prefer has access. If you want a variable to be true, if the user can dance, then prefer can dance. Similarly, if you want something to be true, if the user should provide an address, use should provide address. Notice how the prefixes are naturally appearing in the comments as well. And in terms of the English language, they fall under categories of verbs, but personally I recommend picking it up from whatever sentence you would

05:47

naturally use to describe these variables. One thing that even I strongly feel about is when variables have negation in the name as an example, do not Name a variable is not allowed. Don't use any negation term like the word not within a variable name. It's going to be perfectly fine and easy to understand when the value is true. For example, something is not allowed. However, when it's going to be false, you are going to need to do a double negative mapping in your brain is not allowed, is false, and therefore something is allowed. Things would be much better if we called.

06:21

A variable is allowed and it's very clear when something is not allowed and when something is allowed. And these are the same as the previous conditions, which were a bit hard to understand. Now that we've taken a deep look at bullions in isolation, it allows us to safely look at more attributes of the core JavaScript types. As always, thank you for joining me and I will see you in the next one.

Professional Modern JavaScript

Professional Modern JavaScript

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36