Not Not in Javascript

Javascript has a peculiar way of converting non booleans to a boolean value.

Came across an expression as

return !!current_user;

where current_user will be null unless user has logged in.

Mozilla docs mention that ! operation

Returns false if its single operand can be converted to true; otherwise, returns true.

Examples of expressions that can be converted to false are those that evaluate to null, 0, the empty string (""), or undefined.
If current_user is null

=> !current_user is true

=> !!current_user is false

A null value got converted to false.

Read more on stackoverflow here and here

Leave a Reply

Your email address will not be published. Required fields are marked *