How to Convert Any Values Into A Boolean in Javascript
Posted on: February 17, 2021 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.
Share on social media
//