How to Force A Value Conversion to Boolean in Javascript

In this article, you will learn how to force a value conversion to boolean in Javascript. Let’s say you have 2 variables. In order to…

The post How to Force A Value Conversion to Boolean in Javascript appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli

In this article, you will learn how to force a value conversion to boolean in Javascript.

Let’s say you have 2 variables.

// A string variable named 'a' with value "codesource"
var a = "codesource";

// An empty string variable named 'b'
var b = "";

In order to force a value conversion to boolean, you can use the double bang operator, !!.

// A string variable named 'a' with value "codesource"
var a = "codesource";

// An empty string variable named 'b'
var b = "";

console.log(!!a);
// => true

console.log(!!b);
// => false

Note: The double bang operator, !! functions by returning the truthy value of its operand. The first ! changes the value into its opposite boolean value. The second ! negates the boolean value taken from the first !.

The post How to Force A Value Conversion to Boolean in Javascript appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli


Print Share Comment Cite Upload Translate Updates
APA

Ariessa Norramli | Sciencx (2021-02-21T10:26:42+00:00) How to Force A Value Conversion to Boolean in Javascript. Retrieved from https://www.scien.cx/2021/02/21/how-to-force-a-value-conversion-to-boolean-in-javascript/

MLA
" » How to Force A Value Conversion to Boolean in Javascript." Ariessa Norramli | Sciencx - Sunday February 21, 2021, https://www.scien.cx/2021/02/21/how-to-force-a-value-conversion-to-boolean-in-javascript/
HARVARD
Ariessa Norramli | Sciencx Sunday February 21, 2021 » How to Force A Value Conversion to Boolean in Javascript., viewed ,<https://www.scien.cx/2021/02/21/how-to-force-a-value-conversion-to-boolean-in-javascript/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Force A Value Conversion to Boolean in Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/21/how-to-force-a-value-conversion-to-boolean-in-javascript/
CHICAGO
" » How to Force A Value Conversion to Boolean in Javascript." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/02/21/how-to-force-a-value-conversion-to-boolean-in-javascript/
IEEE
" » How to Force A Value Conversion to Boolean in Javascript." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/02/21/how-to-force-a-value-conversion-to-boolean-in-javascript/. [Accessed: ]
rf:citation
» How to Force A Value Conversion to Boolean in Javascript | Ariessa Norramli | Sciencx | https://www.scien.cx/2021/02/21/how-to-force-a-value-conversion-to-boolean-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.