This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
In this article, you will learn how to convert any values into a boolean in Javascript.
Let’s say you have 4 types of variables.
// 1: A number variable named 'a' with the value 1
var a = 1;
// 2: A string variable named 'b' with the value "codesource"
var b = "codesource";
// 3: An undefined variable named 'c' with the value undefined
var c;
// 4: A null variable named 'd' with the value null
var d = null;
In order to convert any values into a boolean, you can use the Boolean()
method.
var a = 1;
var b = "codesource";
var c;
var d = null;
console.log(Boolean(a));
console.log(Boolean(b));
console.log(Boolean(c));
console.log(Boolean(d));
Note: The Boolean
method functions by converting empty strings and the number zero (0) to false, and others to true.
The post How to Convert Any Values Into A 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-17T06:48:45+00:00) How to Convert Any Values Into A Boolean in Javascript. Retrieved from https://www.scien.cx/2021/02/17/how-to-convert-any-values-into-a-boolean-in-javascript/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.