- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM Node Lists
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
- JavaScript Useful Resources
- JavaScript - Questions And Answers
- JavaScript - Quick Guide
- JavaScript - Functions
- JavaScript - Resources
JavaScript - The Boolean Object
The JavaScript Boolean object represents two values, either "true" or "false". You can create a boolean object using the Boolean() constructor with a 'new' keyword. It takes a value as parameter and returns a boolean object. If value parameter is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. In programming, the if-else statement evaluates either the code of the 'if' block or the 'else' block based on the boolean value of the conditional expression.
Syntax
Use the following syntax to create a boolean object.
const val = new Boolean(value);
Here value is an expression to convert into a Boolean object.
It returns an object containing the boolean value.
You can create a boolean primitive in JavaScript by assigning a boolean value to a variable −
let bool = true;
Boolean Properties
Here is a list of the properties of Boolean object −
Sr.No. | Property & Description |
---|---|
1 | constructor
Returns a reference to the Boolean function that created the object. |
2 | prototype
The prototype property allows you to add properties and methods to an object. |
In the following sections, we will have a few examples to illustrate the properties of Boolean object.
Boolean Methods
Here is a list of the methods of Boolean object and their description.
Sr.No. | Method & Description |
---|---|
1 | toSource()
Returns a string containing the source of the Boolean object; you can use this string to create an equivalent object. |
2 | toString()
Returns a string of either "true" or "false" depending upon the value of the object. |
3 | valueOf()
Returns the primitive value of the Boolean object. |
In the following sections, we will have a few examples to demonstrate the usage of the Boolean methods.
Example: Creating a Boolean Object
In the example below, we have defined the boolObj variable and stored the boolean object.
We used the typeof operator to check the type boolObj variable. In the output, you can observe that the type of the boolObj is the object.
<html> <body> <p id = "output"> </p> <script> const boolObj = new Boolean('true'); //defining boolean object document.getElementById("output").innerHTML = "typof boolObj == " + typeof boolObj; </script> </body> </html>
Output
typof boolObj == object
JavaScript Boolean() Function
The Boolean() function allows the developer to get the boolean value after evaluating the particular expression passed as a parameter.
Boolean(Expression);
Here value is an expression to evaluate and get related boolean values. The Boolean() function returns the true or false based on the expression passed as a parameter.
Example
In the example below, We used the Boolean() function and passed different expressions as a parameter. In the output, you can observe the boolean value returned by the Boolean() function.
<html> <body> <p id = "output"> </p> <script> let res = Boolean(100 > 90); document.getElementById("output").innerHTML = "Boolean(100 > 90) : " + res + "<br>"; res = Boolean(100 < 90); document.getElementById("output").innerHTML = "Boolean(100 < 90) : " + res + "<br>"; res = 100 == 90; document.getElementById("output").innerHTML = "100 == 90 : " + res + "<br>"; </script> </body> </html>
Output
Boolean(100 > 90) : true Boolean(100 < 90) : false 100 == 90 : false
JavaScript Falsy Boolean Values
The Boolean() function returns false for the falsy values. There are six falsy values –false, null, undefined, 0 (zero), "" (empty string), NaN.
Let's look at the example below.
Example
In the code below, we have passed all the falsy values as a parameter of the Boolean() function and printed the returned value from the Boolean() function. The Boolean() function returns false for all 7 values.
<html> <body> <p id = "demo"> </p> <script> document.getElementById("demo").innerHTML = "Boolean(0) : " + Boolean(0) + "<br>" + "Boolean(-0) : " + Boolean(-0) + "<br>" + "Boolean(null) : " + Boolean(null) + "<br>" + "Boolean(undefined) : " + Boolean(undefined) + "<br>" + "Boolean('') : " + Boolean('') + "<br>" + "Boolean(NaN) : " + Boolean(NaN) + "<br>" + "Boolean(false) : " + Boolean(false); </script> </body> </html>
Output
Boolean(0) : false Boolean(-0) : false Boolean(null) : false Boolean(undefined) : false Boolean('') : false Boolean(NaN) : false Boolean(false) : false
All other values are truthy, and the Boolean() function returns true.
Example
In the below code, we passed the truthy values as a Boolean() function parameter. The Boolean() function returns true for all truthy values. Even if we have passed the object and function as a Boolean() function parameter, it returns true.
<html> <body> <p id = "demo"> </p> <script> document.getElementById("demo").innerHTML = "Boolean(1) : " + Boolean(1) + "<br>" + "Boolean(-1) : " + Boolean(-1) + "<br>" + "Boolean('Hello') : " + Boolean('Hello') + "<br>" + "Boolean(true) : " + Boolean(true) + "<br>" + "Boolean(10.99) : " + Boolean(10.99) + "<br>" + "Boolean({name: 'John'}) : " + Boolean({ name: 'John' }) + "<br>" + "Boolean(() => {return 1;}) : " + Boolean(() => { return 1; }); </script> </body> </html>
Output
Boolean(1) : true Boolean(-1) : true Boolean('Hello') : true Boolean(true) : true Boolean(10.99) : true Boolean({name: 'John'}) : true Boolean(() => {return 1;}) : true