Forgot username/password? Don't have an account?
Login with Facebook
Home » Courses » JavaScript 101 » Boolean
Subscribe for updates.
* New to us? log in with Facebook and get a free day pass

JavaScript 101

  1. Intro (03:59)
  2. The Script Tag (04:26)
  3. OOP and DOM (02:45)
  4. What are Variables? (01:37)
  5. Strings (10:39)
  6. Creating Javascript Comments (00:52)
  7. Numbers (09:29)
  8. Boolean (09:10)

Boolean

True and false, the life story of the Boolean data type is that short. In programming we deal a lot with yes or no values, meaning we have to make decisions based on whether something is true or false: this is where Boolean steps in and eases our lives.

We have already established the fact that Strings and Numbers are stored in variable var; so are Booleans. Let’s look at some syntax first so we can determine how exactly they are declared.

var isTotalJerk = false; var isFriend = true;

This is the correct way of declaring and assigning values to a Boolean variable. If you’re wondering how that differs from strings, the devil's in the details! If you look closely, we have not enclosed the words true and false in double quotation marks; if we had written “true” or “false” than they would have been stored as strings.

How do Booleans help us?

How do Booleans help us? We are going to learn another JavaScript function called "confirm();" Every now and then you will need to ask the user a simple yes/no question such as whether they want to continue doing something or not. This is where we use confirm(); it accepts a single string argument and displays a pop up box very similar to alert(); except this pop-up box has two buttons which return either true or false. Let’s look at the syntax first, then we will see its usage and advantage.

var goAhead = confirm(“Do you want to continue?”);

This code will pop up a box on the user's screen where he can either click OK or CANCEL. OK stores True in goAgead while CANCEL stores False.

An Important thing to note here is that the True and False values we are storing in the Boolean variable identify whether goAhead is empty or not. If we simply put strings in it such as “True” or “False” then the variable goAhead will always have something in it but it can’t be efficiently used in an IF check (about which we are about to learn.)

Using Boolean with an IF check

Let’s see how it’s used. (If you'renot familiar with the “IF” check you really must visit the Developer Basics Section and explore our basic developing tutorials; they are specially designed to teach you the details of programming fundamentals and the first one is free!)

var goAhead = confirm(“Do you want to continue?”);

This will store true or false in the variable. Now we can use it within our 'if check' to help us take decisions like this:

if (goAhead) {

alert(“You want to go ahead to swim in that swamp full of alegators”);

}

else

alert(“You DON’T want to go ahead to swim as you are a baby”);

}

What will the previous code do? If the user clicks OK, we'll see YOU WANT TO GO AHEAD, if the user clicks CANCEL we will see YOU DON’T WANT TO GO AHEAD. That easy! Booleans are simple, but so very vital in programming – so please watch this video again if you don't completely understand the concept.

Got A Question?

'I love being Prime'

Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.

Act now while we have available seating

Our on line school is private, affordable and interactive with trainer support.
Act now save your seat before someone else takes yours.
Act now