Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
As mentioned in another answer, when you have code such as if (someVar), you're subject to truthiness. To be more precise, this code will run under the rules of Boolean coercion. In JavaScript, wh...
Answer
#2: Post edited
- As mentioned in [another answer](/posts/291489/291491#answer-291491), when you have code such as `if (someVar)`, you're subject to [truthiness](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). To be more precise, this code will run under the rules of [Boolean coercion](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean#boolean_coercion).
- In JavaScript, when you have `if (someVar)`, the variable `someVar` can be basically "anything" - even a boolean value! If `someVar` is not a boolean, it'll be converted according to some rules:
- - Booleans are returned as-is.
- - `undefined` turns into `false`.
- - `null` turns into `false`.
- - `0`, `-0`, and `NaN` turn into `false`; other numbers turn into `true`.
- - `0n` turns into `false`; other `BigInts` turn into `true`.
- - The empty string `""` turns into `false`; other strings turn into `true`.
- - Symbols turn into `true`.
- - All objects become `true`.
- A little test to check this behaviour:
- ```javascript
- function test(someVar) {
- if (someVar) {
- console.log(someVar, typeof someVar, 'true');
- } else {
- console.log(someVar, typeof someVar, 'false');
- }
- }
- for (const v of [ null, undefined, 0, 1, '', 'abc', [], [1, 2], {} ]) {
- test(v);
- }
- ```
- Output:
- ```
- null object false
- undefined undefined false
- 0 number false
- 1 number true
- string false
- abc string true
- [] object true
- [ 1, 2 ] object true
- {} object true
- ```
Note that **any** object is type-coerced to `true`[^1] (even an empty array or empty object).[^1]: Except, of course, `null` (which is an object - [although it shouldn't](https://stackoverflow.com/posts/comments/27739124) - but it's coerced to `false`)- ---
- ## Now back to the question: how should I check for a boolean value?
- It depends!
- **If you know** that `someVar` is a boolean value (either `true` or `false`, nothing else, and that's guaranteed in another part of the code), then `if (someVar)` is the most straighforward way to check it. You don't need anything else to verify if the value is `true` or `false`.
- On the other hand, if you don't know what type `someVar` is (or you know, and it can be either a boolean or some other type), and you want to **check if the type is boolean**, then you could do something like this:
- ```javascript
- function checkBoolean(someVar) {
- if (typeof someVar === 'boolean' && someVar) {
- console.log('true');
- } else {
- console.log('false or not boolean');
- }
- }
- ```
- But in this case, if `someVar` is `false`, it'll enter the `else` clause (it's a boolean, but it's not `true`). If you want to differentiate between `true`, `false` and "anything else", then it should be:
- ```javascript
- function checkBoolean(someVar) {
- if (typeof someVar === 'boolean') { // it's a boolean value
- // it's either true or false
- if (someVar) {
- console.log('true');
- } else {
- console.log('false');
- }
- } else { // it's not a boolean
- console.log('not boolean');
- }
- }
- ```
- But if you don't care about the type of `someVar` and just want to know if it's "true" or "false" (according to the language's type coercion rules), `if (someVar)` is enough.
- And of course, if you need a different check (such as "*I want empty arrays to be `false`*"), then just change the clause accordingly (ex: `if (Array.isArray(someVar) ? someVar.length > 0 : someVar)` - if it's an array, it can't be empty, otherwise check for truthiness).
- Anyway, you must define exactly what you need: check if the type is boolean, or if the type-coerced value is `true`/`false`, or anything else (ignore `null` and `undefined`, or empty arrays can't be `true`, etc). Then you change the code accordingly.
- ---
- ### Regarding the suggested code
- IMO, `if (typeof(someVar) !== 'undefined' || typeof(someVar) !== null || someVar !== '')` is not a good way to check boolean values. Actually, I believe there's an error in this code. For example, if `someVar` is `null`, it enters the `if` clause:
- ```javascript
- const someVar = null;
- if (typeof(someVar) !== 'undefined' || typeof(someVar) !== null || someVar !== '') {
- console.log('ok');
- } else {
- console.log('not ok');
- }
- ```
- This code prints "ok", because `typeof(null)` is not `null`.
- I believe that `typeof(someVar) !== null` is wrong, because it doesn't make sense: `typeof anything` will never be `null` (according to the [language specification](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-typeof-operator), the result of `typeof` is always a string).
- Actually, it's worse: even if `someVar` is `undefined` or the empty string, this code will print "ok". That's because `typeof(someVar)` is never `null`, thus the condition `typeof(someVar) !== null` is always true. Therefore, this code is not checking anything.
- As mentioned in [another answer](/posts/291489/291491#answer-291491), when you have code such as `if (someVar)`, you're subject to [truthiness](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). To be more precise, this code will run under the rules of [Boolean coercion](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean#boolean_coercion).
- In JavaScript, when you have `if (someVar)`, the variable `someVar` can be basically "anything" - even a boolean value! If `someVar` is not a boolean, it'll be converted according to some rules:
- - Booleans are returned as-is.
- - `undefined` turns into `false`.
- - `null` turns into `false`.
- - `0`, `-0`, and `NaN` turn into `false`; other numbers turn into `true`.
- - `0n` turns into `false`; other `BigInts` turn into `true`.
- - The empty string `""` turns into `false`; other strings turn into `true`.
- - Symbols turn into `true`.
- - All objects become `true`.
- A little test to check this behaviour:
- ```javascript
- function test(someVar) {
- if (someVar) {
- console.log(someVar, typeof someVar, 'true');
- } else {
- console.log(someVar, typeof someVar, 'false');
- }
- }
- for (const v of [ null, undefined, 0, 1, '', 'abc', [], [1, 2], {} ]) {
- test(v);
- }
- ```
- Output:
- ```
- null object false
- undefined undefined false
- 0 number false
- 1 number true
- string false
- abc string true
- [] object true
- [ 1, 2 ] object true
- {} object true
- ```
- Note that **any** object is type-coerced to `true`<sup>**1**</sup> (even an empty array or empty object).
- <sup>**1**: Except, of course, `null` (which is an object - [although it shouldn't](https://stackoverflow.com/posts/comments/27739124) - but it's coerced to `false`)</sup>
- ---
- ## Now back to the question: how should I check for a boolean value?
- It depends!
- **If you know** that `someVar` is a boolean value (either `true` or `false`, nothing else, and that's guaranteed in another part of the code), then `if (someVar)` is the most straighforward way to check it. You don't need anything else to verify if the value is `true` or `false`.
- On the other hand, if you don't know what type `someVar` is (or you know, and it can be either a boolean or some other type), and you want to **check if the type is boolean**, then you could do something like this:
- ```javascript
- function checkBoolean(someVar) {
- if (typeof someVar === 'boolean' && someVar) {
- console.log('true');
- } else {
- console.log('false or not boolean');
- }
- }
- ```
- But in this case, if `someVar` is `false`, it'll enter the `else` clause (it's a boolean, but it's not `true`). If you want to differentiate between `true`, `false` and "anything else", then it should be:
- ```javascript
- function checkBoolean(someVar) {
- if (typeof someVar === 'boolean') { // it's a boolean value
- // it's either true or false
- if (someVar) {
- console.log('true');
- } else {
- console.log('false');
- }
- } else { // it's not a boolean
- console.log('not boolean');
- }
- }
- ```
- But if you don't care about the type of `someVar` and just want to know if it's "true" or "false" (according to the language's type coercion rules), `if (someVar)` is enough.
- And of course, if you need a different check (such as "*I want empty arrays to be `false`*"), then just change the clause accordingly (ex: `if (Array.isArray(someVar) ? someVar.length > 0 : someVar)` - if it's an array, it can't be empty, otherwise check for truthiness).
- Anyway, you must define exactly what you need: check if the type is boolean, or if the type-coerced value is `true`/`false`, or anything else (ignore `null` and `undefined`, or empty arrays can't be `true`, etc). Then you change the code accordingly.
- ---
- ### Regarding the suggested code
- IMO, `if (typeof(someVar) !== 'undefined' || typeof(someVar) !== null || someVar !== '')` is not a good way to check boolean values. Actually, I believe there's an error in this code. For example, if `someVar` is `null`, it enters the `if` clause:
- ```javascript
- const someVar = null;
- if (typeof(someVar) !== 'undefined' || typeof(someVar) !== null || someVar !== '') {
- console.log('ok');
- } else {
- console.log('not ok');
- }
- ```
- This code prints "ok", because `typeof(null)` is not `null`.
- I believe that `typeof(someVar) !== null` is wrong, because it doesn't make sense: `typeof anything` will never be `null` (according to the [language specification](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-typeof-operator), the result of `typeof` is always a string).
- Actually, it's worse: even if `someVar` is `undefined` or the empty string, this code will print "ok". That's because `typeof(someVar)` is never `null`, thus the condition `typeof(someVar) !== null` is always true. Therefore, this code is not checking anything.
#1: Initial revision
As mentioned in [another answer](/posts/291489/291491#answer-291491), when you have code such as `if (someVar)`, you're subject to [truthiness](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). To be more precise, this code will run under the rules of [Boolean coercion](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean#boolean_coercion). In JavaScript, when you have `if (someVar)`, the variable `someVar` can be basically "anything" - even a boolean value! If `someVar` is not a boolean, it'll be converted according to some rules: - Booleans are returned as-is. - `undefined` turns into `false`. - `null` turns into `false`. - `0`, `-0`, and `NaN` turn into `false`; other numbers turn into `true`. - `0n` turns into `false`; other `BigInts` turn into `true`. - The empty string `""` turns into `false`; other strings turn into `true`. - Symbols turn into `true`. - All objects become `true`. A little test to check this behaviour: ```javascript function test(someVar) { if (someVar) { console.log(someVar, typeof someVar, 'true'); } else { console.log(someVar, typeof someVar, 'false'); } } for (const v of [ null, undefined, 0, 1, '', 'abc', [], [1, 2], {} ]) { test(v); } ``` Output: ``` null object false undefined undefined false 0 number false 1 number true string false abc string true [] object true [ 1, 2 ] object true {} object true ``` Note that **any** object is type-coerced to `true`[^1] (even an empty array or empty object). [^1]: Except, of course, `null` (which is an object - [although it shouldn't](https://stackoverflow.com/posts/comments/27739124) - but it's coerced to `false`) --- ## Now back to the question: how should I check for a boolean value? It depends! **If you know** that `someVar` is a boolean value (either `true` or `false`, nothing else, and that's guaranteed in another part of the code), then `if (someVar)` is the most straighforward way to check it. You don't need anything else to verify if the value is `true` or `false`. On the other hand, if you don't know what type `someVar` is (or you know, and it can be either a boolean or some other type), and you want to **check if the type is boolean**, then you could do something like this: ```javascript function checkBoolean(someVar) { if (typeof someVar === 'boolean' && someVar) { console.log('true'); } else { console.log('false or not boolean'); } } ``` But in this case, if `someVar` is `false`, it'll enter the `else` clause (it's a boolean, but it's not `true`). If you want to differentiate between `true`, `false` and "anything else", then it should be: ```javascript function checkBoolean(someVar) { if (typeof someVar === 'boolean') { // it's a boolean value // it's either true or false if (someVar) { console.log('true'); } else { console.log('false'); } } else { // it's not a boolean console.log('not boolean'); } } ``` But if you don't care about the type of `someVar` and just want to know if it's "true" or "false" (according to the language's type coercion rules), `if (someVar)` is enough. And of course, if you need a different check (such as "*I want empty arrays to be `false`*"), then just change the clause accordingly (ex: `if (Array.isArray(someVar) ? someVar.length > 0 : someVar)` - if it's an array, it can't be empty, otherwise check for truthiness). Anyway, you must define exactly what you need: check if the type is boolean, or if the type-coerced value is `true`/`false`, or anything else (ignore `null` and `undefined`, or empty arrays can't be `true`, etc). Then you change the code accordingly. --- ### Regarding the suggested code IMO, `if (typeof(someVar) !== 'undefined' || typeof(someVar) !== null || someVar !== '')` is not a good way to check boolean values. Actually, I believe there's an error in this code. For example, if `someVar` is `null`, it enters the `if` clause: ```javascript const someVar = null; if (typeof(someVar) !== 'undefined' || typeof(someVar) !== null || someVar !== '') { console.log('ok'); } else { console.log('not ok'); } ``` This code prints "ok", because `typeof(null)` is not `null`. I believe that `typeof(someVar) !== null` is wrong, because it doesn't make sense: `typeof anything` will never be `null` (according to the [language specification](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-typeof-operator), the result of `typeof` is always a string). Actually, it's worse: even if `someVar` is `undefined` or the empty string, this code will print "ok". That's because `typeof(someVar)` is never `null`, thus the condition `typeof(someVar) !== null` is always true. Therefore, this code is not checking anything.