boolean check is not working in NodeJS

check if string is boolean javascript
string to boolean javascript
boolean example
javascript function return boolean
javascript check if variable is true
check boolean java
typescript boolean

I am developing the node js API and I a querying data by the URL


This is the function who is responsible for handling this request

 public async get_poset_list(userDeta: hs_I_fb_en_user_auth_paylode,pageId:string,asking_post:boolean,asking_responce:boolean,maxSort:number,minSort:number,limit:number):Promise<hs_I_fb_en_post_return[]>{
        try {
            hs_d_w("Is asking post: - "+asking_post);
            hs_d_w("Limit: - "+limit);
                hs_d_w("Asking post true");
                if(minSort<=-1 && maxSort<=-1){
                    hs_d_w("Asking post Defolt");
                    return this._postQueryes.get_only_poses(pageId,limit);
                        hs_d_w("Asking post MIn");
                        return this._postQueryes.get_only_poses_min(pageId,minSort,limit);
                        hs_d_w("Asking post Max");
                        return this._postQueryes.get_only_poses_max(pageId,maxSort,limit);
                    hs_d_w("Asking post None");
                    return [];
                if(minSort<=-1 && maxSort<=-1){
                    hs_d_w("Asking talk Defolt");
                    return this._postQueryes.get_only_talkes(pageId,limit);
                        hs_d_w("Asking talk min");
                        return this._postQueryes.get_only_talkes_min(pageId,minSort,limit);
                        hs_d_w("Asking talk max");
                        return this._postQueryes.get_only_talkes_max(pageId,maxSort,limit);
                    hs_d_w("Asking talk none");
                    return [];
        } catch (e) {
            hs_d_w("get_poset_list : " + e);
            return Promise.reject(e)

Now if I call set asking_post=false or asking_post=true it allways call the main else area of this function

return this._postQueryes.get_only_talkes(pageId,limit);

This one.

I don't understand why it's happening? Can anyone please help me on this?

When you get something from the req.query it will always return a String. So, make sure to convert it to boolean using

const variable = (variable == 'true')
// or
const variable = (variable === 'true')

On a side note, when a variable is boolean you don't have to check explicitly with ===. This will also work

if(foo) {

} else {


EDIT: as @Kamalakannan said Boolean('string') will not work. My apologies.

boolean in an if statement, For example: // The DOM ready check for Internet Explorer function doScrollCheck() {  False: Because the boolean equivalent of a is false. Conclusion. Try to avoid using boolean values comparison with "==". It will work for you in many cases but it is not a right way to perform a condition check.

You can simply convert it with JSON.parse.

const x = JSON.parse('true');
const y = JSON.parse('false');

It will return boolean values for both.

Boolean, You can use the Boolean() function to find out if an expression (or a variable) is true: Example. Boolean(10 > 9) // returns true. Try it Yourself ». Or even easier:  We can convert values to boolean with the Boolean function, and the language converts everything to boolean when needed, according to the following rules: false, 0 , empty strings "", NaN , null , and undefined all evaluate to false .

Query params are considered as strings. So if you check with ===, it will be falsy.

Do string comparison, like if ("true" === asking_post) or if ("false" === asking_post)

Boolean(asking_post) will always return true for string values

const t = Boolean("true");
const f = Boolean("false");

console.log("Value of 'true':", t);
console.log("Value of 'false':", f);

JavaScript Booleans, It will work for you in many cases but it is not a right way to perform a condition check. heart 20 unicorn 6 reading list 32 It is possible to use a couple of NOT operators in series to explicitly force the conversion of any value to the corresponding boolean primitive. The conversion is based on the "truthyness" or "falsyness" of the value (see truthy and falsy). The same conversion can be done through the Boolean function.

When you get any values from request you always get String type. So you need to convert it into Boolean first. Or just check with String.

You can do this: (I personally preferred)

var isTrue = (asking_post == 'true');

But please be caution to use following method:

var isTrue = Boolean("false");  // return true

var isTrue = !!"false";  // return true

Any string which is not empty will give you true by using the above methods.

Avoid boolean values comparison with "==" in JavaScript, JavaScript treats empty string (""), 0, undefined and null as false. Everything else is true. Example: Boolean. var bool1 = new Boolean("");  When using the === operator, equal booleans are not equal, because the === operator expects equality in both type and value.

JavaScript Boolean, How to check for null in JavaScript Because of a historical bug, typeof null in JavaScript This contrasts null from the similar primitive value undefined , which is an null is a falsy value (i.e. it evaluates to false if coerced to a boolean)” — Josh  Working with environment variables is a great way to configure different aspects of your Node.js application. Many cloud hosts (Heroku, Azure, AWS,, etc.) and Node.js modules use environment variables. Hosts, for example, will set a PORT variable that specifies on which port the server should listen to properly work. Modules might have

How to Check for null in JavaScript, All comparison operators return a boolean value: On the other hand, the equality check == for undefined and null is defined such that, without  Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object. Any object of which the value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement.

Comparisons, Normally, the Node.js process will exit when there is no work scheduled, but a listener Set of flags allowable within the NODE_OPTIONS environment variable. Node.js may throw an error when the value is not a string, number, or boolean. Working with Different Filesystems. Node.js exposes many features of the filesystem. But not all filesystems are alike. The following are suggested best practices to keep your code simple and safe when working with different filesystems. Filesystem Behavior. Before you can work with a filesystem, you need to know how it behaves.

  • if((!!asking_post) === true)
  • Boolean("string") will always return true, be it "true" or "false", so it won't work
  • asking_post == 'true' - we can use === here
  • Yes, and we can also use == too.
  • If asking_post is expected to be string why would we use ==? It's confusing to everyone and implies redundant type casting.