Boolean: True/False

Examples

lightswitch

Example

subway turnstyle

Boolean Type

A boolean like a string or number is a data type in JavaScript. Strings are anything with single or double quotes around them.


"A string"
'Also a string'
1 //A number
true //A boolean
false //Another boolean
            

What will this print to the console?


var hungry = true;
var full = false;
console.log(hungry);
console.log(full);
          

Boolean experession

A boolean experession is something that results in true or false

JS Bin on jsbin.com

What will this print to the console


if(5 > 4){
console.log("Yes");
} else {
console.log("NO");
}
          

What will this print to the console?


if(10 > 11){
console.log("10 is greater than 11");
}else{
console.log("10 is not greater than 11")
}
          

Boolean Operators

> means greater than

< means less than

=== means equal to

So Does == But always use three equal signs

practice conditionals

Write a function that takes in two numbers and prints out the larger number to the console.

JS Bin on jsbin.com

More Boolean Operators

<= means less than or equal to

>= means greater than or equal to

!== means not equal to

Putting a ! in front of any means its opposite

&& means AND

|| means OR

What does this print out


if(!(5 > 4)){
console.log("Yes");
}else {
console.log("NO");
}
          

What does this print out?


if( 5 > 4 && 6 > 5){
console.log("Both are true");
}else{
console.log("Both were not true");
}
          

What does this print out?


if(5 > 4 || 3 > 4){
console.log("At least one was true");
}else{
console.log("None were true");
}
          

What does this print out?


if( 5 !== 4){
console.log("That's right 5 is not equal to 4")
}else {
console.loge("5 is equal to 4")
}
          

You Do!

  • A function called "maxOfThree()" that takes three numbers and returns the largest of the three.
  • A function called "longestWord()" that takes two words and returns the longest word? Hint: How do you find out how long a string is in JavaScript?
  • A function called "assignGrade()" that takes in a number and returns a grade of "A", "B", "C", "D","F". Test to make sure that you get the correct grade for each.
    100 - 90 A
    89-80 B
    79 - 70 C
    69-60 D
    60 or less F

Bonus: The Pluralizer

Write a function called "pluralizer()" that:

  • takes 2 arguments: a number and a noun
  • returns the number and the pluralized form of the noun like: "5 cats", "1 dog"
  • Do not return "1 dogs"
  • Call it with a few numbers and nouns and make sure that it works for words that you make plural by just writing an s
  • Double Bonus: What do you do for words like "goose" whose plural is "geese"