How to Convert Any Values Into A Boolean in Javascript

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. In…

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

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » How to Convert Any Values Into A Boolean in Javascript." Ariessa Norramli | Sciencx - Wednesday February 17, 2021, https://www.scien.cx/2021/02/17/how-to-convert-any-values-into-a-boolean-in-javascript/
HARVARD
Ariessa Norramli | Sciencx Wednesday February 17, 2021 » How to Convert Any Values Into A Boolean in Javascript., viewed ,<https://www.scien.cx/2021/02/17/how-to-convert-any-values-into-a-boolean-in-javascript/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Convert Any Values Into A Boolean in Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/17/how-to-convert-any-values-into-a-boolean-in-javascript/
CHICAGO
" » How to Convert Any Values Into A Boolean in Javascript." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/02/17/how-to-convert-any-values-into-a-boolean-in-javascript/
IEEE
" » How to Convert Any Values Into A Boolean in Javascript." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/02/17/how-to-convert-any-values-into-a-boolean-in-javascript/. [Accessed: ]
rf:citation
» How to Convert Any Values Into A Boolean in Javascript | Ariessa Norramli | Sciencx | 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.

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