- JavaScript Basics
- JavaScript Tutorial
- JavaScript: where to write
- JavaScript: how to display
- JavaScript: keywords
- JavaScript: comments
- JavaScript: variables
- JavaScript: operators
- JavaScript: data types
- JavaScript Conditional Statements
- JavaScript: if-else
- JavaScript: switch
- JavaScript: for loop
- JavaScript: while loop
- JavaScript: do-while loop
- JavaScript: break and continue
- JavaScript Popup Boxes
- JavaScript: alert box
- JavaScript: confirm box
- JavaScript: prompt box
- JavaScript Popular Topics
- JavaScript: functions
- JavaScript: innerHTML
- JavaScript: getElementById()
- JavaScript: getElementsByClassName()
- JavaScript: getElementsByName()
- JavaScript: getElementsByTagName()
- JavaScript: querySelector()
- JavaScript: querySelectorAll()
- JavaScript: document.write()
- JavaScript: console.log()
- JavaScript: boolean
- JavaScript: events
- JavaScript: Math object
- JavaScript: Math.random()
- JavaScript: Number()
- JavaScript: parseInt()
- JavaScript: parseFloat()
- JavaScript Arrays
- JavaScript: array
- JavaScript: find length of array
- JavaScript: add element at beginning
- JavaScript: add element at end
- JavaScript: remove first element
- JavaScript: remove last element
- JavaScript: get first index
- JavaScript: get last index
- JavaScript: reverse an array
- JavaScript: sort an array
- JavaScript: concatenate arrays
- JavaScript: join()
- JavaScript: toString()
- JavaScript: from()
- JavaScript: check if value exists
- JavaScript: check if array
- JavaScript: slice an array
- JavaScript: splice()
- JavaScript: find()
- JavaScript: findIndex()
- JavaScript: entries()
- JavaScript: every()
- JavaScript: fill()
- JavaScript: filter()
- JavaScript: forEach()
- JavaScript: map()
- JavaScript Strings
- JavaScript: string
- JavaScript: length of string
- JavaScript: convert to lowercase
- JavaScript: convert to uppercase
- JavaScript: string concatenation
- JavaScript: search()
- JavaScript: indexOf()
- JavaScript: search() vs. indexOf()
- JavaScript: match()
- JavaScript: match() vs. search()
- JavaScript: replace()
- JavaScript: toString()
- JavaScript: String()
- JavaScript: includes()
- JavaScript: substr()
- JavaScript: slice string
- JavaScript: charAt()
- JavaScript: repeat()
- JavaScript: split()
- JavaScript: charCodeAt()
- JavaScript: fromCharCode()
- JavaScript: startsWith()
- JavaScript: endsWith()
- JavaScript: trim()
- JavaScript: lastIndexOf()
- JavaScript Date and Time
- JavaScript: date and time
- JavaScript: Date()
- JavaScript: getFullYear()
- JavaScript: getMonth()
- JavaScript: getDate()
- JavaScript: getDay()
- JavaScript: getHours()
- JavaScript: getMinutes()
- JavaScript: getSeconds()
- JavaScript: getMilliseconds()
- JavaScript: getTime()
- JavaScript: getUTCFullYear()
- JavaScript: getUTCMonth()
- JavaScript: getUTCDate()
- JavaScript: getUTCDay()
- JavaScript: getUTCHours()
- JavaScript: getUTCMinutes()
- JavaScript: getUTCSeconds()
- JavaScript: getUTCMilliseconds()
- JavaScript: toDateString()
- JavaScript: toLocaleDateString()
- JavaScript: toLocaleTimeString()
- JavaScript: toLocaleString()
- JavaScript: toUTCString()
- JavaScript: getTimezoneOffset()
- JavaScript: toISOString()
- JavaScript Regular Expression
- JavaScript: regular expression
- JavaScript: RegEx . (dot)
- JavaScript: RegEx \w and \W
- JavaScript: RegEx \d and \D
- JavaScript: RegEx \s and \S
- JavaScript: RegEx \b and \B
- JavaScript: RegEx \0
- JavaScript: RegEx \n
- JavaScript: RegEx \xxx
- JavaScript: RegEx \xdd
- JavaScript: RegEx quantifiers
- JavaScript: RegEx test()
- JavaScript: RegEx lastIndex
- JavaScript: RegEx source
- JavaScript Programs
- JavaScript Programs
JavaScript Boolean
Boolean values can be represented in JavaScript in two different ways: as primitive boolean values and as Boolean objects. A basic value that denotes either the "true" or "false" state is known as a primitive boolean value. These values are typically applied in logical operators, conditional statements, and other expressions that yield boolean results.
So let's discuss the primitive boolean values first, and then we will cover Boolean objects after that. The Boolean() function is used to create Boolean objects and is also described briefly at the end of this article.
A boolean is a data type in JavaScript that represents the value "true" or "false." It is used to express logical values, which are indispensable in programming for decision-making, conditional execution, and other purposes.
The true and false keywords can be used to create boolean values, or they can result from a comparison or logical operation. For instance:
<!DOCTYPE html> <html> <body> <script> // Simulate user authentication const isAuthenticated = true; // Check if user is logged in if (isAuthenticated) { console.log("Welcome, user!"); } else { console.log("Please log in to continue."); } </script> </body> </html>
Since "isAuthenticated" is set to "true" the condition if the if statement evaluates to true, which forces the program flow to enter the if's body that executes the console.log() statement that prints the text "Welcome, user!" to the console. Therefore, the following snapshot shows the sample output produced by this example:
JavaScript Boolean Object
A Boolean object is a convenient container for a simple boolean value that adds useful features. The Boolean constructor accepts a single argument and returns a new Boolean object; it is used to create Boolean objects. The only values that are converted to false are false, 0, "", null, undefined, and NaN. All other values, including non-empty strings, are converted to true when used as an argument for the Boolean constructor. As an example:
var boolObj = new Boolean(true);
Now the variable "boolObj" can be considered a "Boolean" object.
The primary distinction between a primitive boolean value and a Boolean object is how JavaScript treats them. Primitive boolean values are preferred in most cases because they are faster to create and use and take up less memory than Boolean objects.
Care must be taken when comparing a Boolean object to a primitive boolean value using the strict equality operator (===). Due to the fact that the strict equality operator does not perform type coercion, a Boolean object with the value true is not strictly equal to a primitive boolean value with the same value. Consider the following code demonstrating this concept:
<!DOCTYPE html> <html> <body> <script> var boolObj = new Boolean(true); console.log(boolObj === true); var boolVal = true; console.log(boolVal === true); </script> </body> </html>
false true
JavaScript Boolean object properties
The list given below describes the properties of the Boolean object in JavaScript.
- Boolean.prototype: A reference to the "Boolean" constructor's prototype.
- Boolean.prototype.constructor: The constructor function that built the instance of the "Boolean" object is referenced by the this property.
JavaScript Boolean object methods
The list given below describes the methods of the Boolean object in JavaScript.
- Boolean.toString(): Returns a string representation of a boolean value.
- Boolean.valueOf(): Returns a "Boolean" object's primitive boolean value.
- Boolean.prototype.toString(): Returns the "Boolean" object as a string.
- Boolean.prototype.valueOf(): Returns the "Boolean" object's primitive boolean value.
JavaScript Boolean() function
The JavaScript Boolean() function is used to create Boolean objects and to determine whether an expression or variable is true or false. Here's an illustration:
<!DOCTYPE html> <html> <body> <script> var x = Boolean(0); console.log(x); var y = Boolean(1); console.log(y); var z = Boolean(""); console.log(z); var w = Boolean("hello"); console.log(w); var boolObj = new Boolean(true); console.log(boolObj); var boolVal = Boolean(true); console.log(boolVal); </script> </body> </html>
The output should be:
The first "console.log()" statement outputs "false", because 0 is falsy, the second outputs "true", because 1 is truthy, the third outputs "false", because an empty string is falsy, and the fourth outputs "true" because a non-empty string is truthy. Whereas the fifth outputs "Boolean {true}," indicating that the "true" is from the boolean object and not the primitive boolean value. Finally, the last, or sixth, console.log statement outputs "true" using Boolean() with "true" as its argument.
« Previous Tutorial Next Tutorial »