Presentation is loading. Please wait.

Presentation is loading. Please wait.

'Boolean' data type.

Similar presentations


Presentation on theme: "'Boolean' data type."— Presentation transcript:

1 'Boolean' data type

2 Learning Objectives By the end of this lecture, you should be able to:
Understand a new data type: Boolean Appreciate the importance and benefit of Boolean values Know how to use Boolean values in your code

3 New Data Type: 'Boolean' Up to this point, we have learned a few key data types: string, int, and float. We are going to add an additional – and very important – data type to our repertoire: 'Boolean'. This data type holds only one of two possible values: true or false. Pretty much every programming language makes extensive use of this data type. However, they may refer to the values differently. Some languages choose '0' for false, and non-zero for everything else. Others refer to them as True or False (i.e. capitalized first letter). Suppose we wanted to keep track of whether someone was eligible to vote. We will create a variable: var eligibleToVote; Now we could store the value in a variety of different ways: We could store it as a string: eligibleToVote = "yes"; We could store it as an integer: eligibleToVote=1; However, we'd have to know and keep track of how I chose to represent these values. For example, suppose another programmer who stored the variable as a string, chose to say: eligibleToVote="YES" or perhaps eligibleToVote="Y", or how about: eligibleToVote="Yes" Similarly with numbers: Does 1 mean they are eligible? If so, what represents that they are not eligible? A 2? A 0? A negative number? Because true and false are so important in programming, every language has some way of representing this value. In many languages including JavaScript, the datatype is referred to as 'Boolean'. Some languages call it 'bool'. There are other variations out there as well.

4 Using a Boolean JavaScript Booleans store their values as either true or false. So now if we wanted to keep track of a user's eligibility to vote, we might code: var eligibleToVote = true; NOTE: Do not put quotes around the true. If you do, then you will be storing a string that holds the characters t-r-u-e. You will not be storing a Boolean value, you will be storing a string. Booleans are extremely common and useful in programming. For example, in form validation, you might create a variable called 'isFormValid'. If, at any point, in your validation, something is wrong, you can set this Boolean to false. Then at the end of the validation, if the Boolean is false, your code can refuse to proceed with the submission of the form. var isFormValid = true; if ( user forgot to enter address ) isFormValid = false; else if ( user entered an invalid character ) else if ( etc etc ) Then, somewhere towards the end of your code: if (isFormValid == true) //Process the form else alert('Something is wrong with your form');

5 Booleans inside conditionals
One of the most common places you will encounter Booleans is inside conditional expressions. Recall that a conditional must always evaluate to true or false. if ( conditional ) ... Therefore we can have a variable as discussed previously that keeps track of whether a user is eligible to vote: var eligibleToVote = true; At some point in the program, we might have code similar to the following: if ( eligibleToVote == true ) alert('Congrats, you can vote!'); else if ( eligibleToVote == false ) alert('Sorry, you can not vote.');

6 Leaving out ==true or ==false in a conditional
Again, recall that a conditional must ultimately evaluate to true or false if ( conditional ) ... Therefore if we have a variable such as a Boolean inside the conditional, we can leave out the '==true' or '==false': var eligibleToVote = true; if ( eligibleToVote ) //this will work just fine! alert('Congrats, you can vote!'); else alert('Sorry, you can not vote.'); For now, however, I would recommend you include '==true' or ' ==false' inside your conditionals until you get very comfortable with the data type.

7 Example boolean_example.htm museum_admission_validation_boolean.htm


Download ppt "'Boolean' data type."

Similar presentations


Ads by Google