
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to convert a value into Boolean in JavaScript?
In this article we are going to discuss how to convert a value into Boolean in JavaScript.
In JavaScript programming we sometimes need values like these ‘YES / NO, ‘ON / OFF’, ‘TRUE / FALSE’. So, JavaScript provides Boolean() method, which helps us to convert a value into Boolean. A Boolean is a value that can return either true/false. There are two ways using which we can convert values into Boolean. One way is to use Boolean() method and the other way is to use the !! notation.
Let’s understand this concept better with the help of the examples further in this article.
Syntax
The syntax to display the conversion of value to Boolean using Boolean method and operator is shown below.
Boolean(value) or !!(value)
Example 1
The following is an example program to convert a value to Boolean using Boolean() method.
<html> <body> <script> const isTrue = 'Golden State Warriors'; document.write(new Boolean(isTrue)); document.write("</br>"); document.write(Boolean(isTrue)); </script> </body> </html>
On executing the above code, the following output is generated.
Example 2
The following is another example program to convert a value to Boolean using Boolean() method.
<!DOCTYPE html> <html> <head> <title>To display a value in boolean</title> </head> <body style="text-align : center"> <h3>Convert a value into Boolean</h3> <p id="bool"></p> <script> var value1 = 0; var value2 = ''; var value3 = null; var value4 = 'Hello'; var value5 = 1234; document.getElementById("bool").innerHTML = "The boolean value for the value (0) is : "+Boolean(value1)+'<br/>'+"The boolean value for the value ('') is : "+Boolean(value2)+'<br/>'+"The boolean value for the value (null) is : "+Boolean(value3)+'<br/>'+"The boolean value for the value ('Hello') is : "+Boolean(value4)+'<br/>'+"The boolean value for the value (1234) is : "+Boolean(value5); </script> </body> </html>
On executing the above code, the following output is generated.
Example 3
Let’s look at an example program to convert a value to Boolean using !! notation.
<!DOCTYPE html> <html> <head> <title>To display a value in boolean</title> </head> <body style="text-align : center"> <h3>Convert a value into Boolean using !! Notation.</h3> <p id="bool"></p> <script> var value1 = 0; var value2 = ""; var value3 = NaN; var value4 = 'Tutorials Point'; var value5 = 1234; document.getElementById("bool").innerHTML = "The boolean value for the value (0) is : "+!!value1+'<br/>'+"The boolean value for the value ('') is : "+!!value2+'<br/>'+"The boolean value for the value (NaN) is : "+!!value3+'<br/>'+"The boolean value for the value ('Tutorials Point') is : "+!!value4+'<br/>'+"The boolean value for the value (1234) is : "+!!value5; </script> </body> </html>
On executing the above code, the following output is generated.
- Related Articles
- How to convert a boolean value to string value in JavaScript?
- How to convert a Boolean to Integer in JavaScript?
- Java Program to convert boolean value to Boolean
- How to convert Boolean to String in JavaScript?
- How to convert Boolean to Number in JavaScript?
- How to convert Number to Boolean in JavaScript?
- How to convert String to Boolean in JavaScript?
- JavaScript Convert a string to boolean
- How can I convert a string to boolean in JavaScript?
- Convert a specified value to an equivalent Boolean value in C#
- Explain non-boolean value coercion to a boolean one in JavaScript?
- Swift program to convert boolean variable into string
- C++ Program to convert Boolean Variables into String
- Golang Program to convert boolean variables into string
- Haskell Program to convert boolean variables into string
