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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.