02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Boolean

We have learned and used all but one last primitive variable. Are you ready to see it in action? Booleans are simple! They are really only place-holders that can hold only one of two possible values: true or false (0 or 1, yes or no...)

Boolean values can be used to state whether a statement is right or wrong. It has only two options: true or false. We may also take this as 0 or 1 if we are using some other programming languages like c and c++. A Boolean value is like a switch which has only two options: on or off.

Assigning and working with Boolean Variables

We can assign a Boolean value to a variable using var a:Boolean = true;. This means the variable is declared as a Boolean value and the option true is assigned to it. We can't assign any other value (such as string or number) to this Boolean variable other than true or false (0 or 1.) However, if we want to change the Boolean value from true to false later in the program, we can type a= false. This will turn the Boolean value of the variable a to false. Remember that these Boolean values (true or false) should be written directly without any double or single quotes.

Video Transcript

00:00Now the last variable I want to talk about really briefly, the last type I want to talk about is a Boolean type.
00:07A Boolean type by definition could only have two values.
00:12While numbers and strings are open-ended, a Boolean value could only have two values, true or false,
00:21which in some languages it would be 0 or 1, which is basically the same.
00:26It basically says true or false, yes or no.
00:30Boolean values are called also flags because they flag out if something true is true or not.
00:36There's only two options, it's a switch.
00:39The way we create Booleans is the same way we're going to call it B.
00:45We're going to open a colon and then we're going to type Boolean with our B as uppercase.
00:51And we've created a Boolean.
00:54Now Booleans are all true or false and it's not inside of a string.
00:59So we could define our Boolean to equal true or we could define our Boolean to be equal false.
01:07So then if we wanted to make our Boolean equal false, let's comment this out.
01:12So let's first see if we trace our Boolean, what would happen?
01:21Then it would tell us that it's true.
01:23Now if we take our Boolean B variable and say that it's false.
01:28And notice how I'm not putting this inside of any brackets.
01:33I'm not putting inside of any double quotations.
01:35I'm just typing directly false.
01:38And if I trace it out again, now my Boolean value would be equal to false.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!