Booleans are used to represent logical entities that have only two possible values, true or false.
Declaration and Assignment
Boolean variables are declared and assigned a value using the following syntax:
let isSunny = true
let isRaining = false
Here, isSunny
and isRaining
are Boolean variables that can only have a value of true
or false
. These variables can be used in conditional statements to control the flow of a program.
Conditional Statements
Conditional statements are an essential part of programming logic, allowing one to create programs that can make decisions and take different actions based on the values of variables and other data. The if
statement is the most basic conditional statement in JavaScript - executes a block of code if a condition is true:
if (isSunny) {
console.log("It's a beautiful day!")
} else if (isRaining) {
console.log("Better bring an umbrella.")
} else {
console.log("I'm not sure what the weather is like.")
}
In this example, if isSunny
is true
, the program will print "It's a beautiful day!" to the console. If isRaining
is true
, the program will print "Better bring an umbrella." to the console. If both isSunny
and isRaining
are false
, the program will print "I'm not sure what the weather is like." to the console.
Comparison Operators
Comparison operators in JavaScript return Boolean values and are commonly used in conditional statements to compare values and make decisions based on the results. Several comparison operators are available, including >
, >=
, <
, <=
, ==
, and !=
:
let x = 5
let y = 10
let result = x < y // result is true
Here, the less than operator, <
, compares the values of x
and y
, and returns a Boolean value of true
because x
is less than y
.
Boolean Functions
JavaScript provides several built-in functions that return Boolean values, such as isNaN()
and isFinite()
. Custom functions can also be created to return Boolean values based on specific conditions:
function isEven(number) {
return number % 2 === 0
}
isEven(4) // returns: true
isEven(5) // returns: false
Here, the isEven()
function takes a number as an argument and returns true
if the number is even, and false
otherwise.
Truthy and Falsy Values
In JavaScript, some values are considered "truthy" and others are considered "falsy" when evaluated in a Boolean context. Truthy values include non-empty strings, non-zero numbers, and objects, while falsy values include empty strings, zero, null
, undefined
, and NaN
.
if ("hello") {
console.log("This will be executed.")
}
if (0) {
console.log("This will not be executed.")
}
Here, the first if
statement will be executed because the string "hello"
is a truthy value, while the second if
statement will not be executed because the number 0
is a falsy value. Understanding truthy and falsy values is important when working with Boolean data types in JavaScript.
Truthy values:
-
"hello"
(a non-empty string) -
42
(a non-zero number) -
[]
(an empty array)
Falsy values:
-
""
(an empty string) -
0
(the number zero) null
undefined
-
NaN
(Not a Number)
Take Away
Booleans are a fundamental data type in JavaScript that are used extensively in logical expressions and conditional statements.
Top comments (0)