JavaScript has two boolean values: true and false.
console.log(true); // prints the boolean value true to the console
console.log(false); // prints the boolean value false to the console
Boolean conditions are statements that return true or false (i.e. statements that return boolean values). Here is an example of a boolean condition that returns true:
Boolean(2 < 5); // true
The same boolean condition can be written with this shorthand notation:
2 < 5; // true
Boolean conditions can also return false:
Boolean(2 > 5); // false
2 > 5; // false
Booleans conditions can be used to see if two numbers are the same:
console.log(44 === 44); // true because the numbers are equal
Boolean conditions can be used to assess if two strings are the same:
console.log("bob" === "sam"); // false
The string "bob" is different from the string "sam", so the boolean condition returns false. The boolean condition returns true if the strings are the same:
console.log("aaa" === "aaa"); // true
Question | Click to View Answer |
What are the two boolean values in the JavaScript programming language? |
true and false |
What does the following expression print to the console? console.log(Boolean(3 > 1));
|
true
The number 3 is greater than the number 1, so the boolean condition returns true. Boolean conditions are statements that return true or false. |
What does the following expression print to the console? console.log(4 === 4);
|
true
The number 4 equals the number 4, so the boolean condition returns true. |
What does the following expression print to the console? console.log("cat" === "cat");
|
true
The string "cat" is the same as the other string "cat", so the boolean condition returns true. |
What does the following expression print to the console? console.log("fork" === "plate");
|
false
The string "fork" is different than the string "plate", so the boolean condition returns false. |