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

Aly Chiman

Aly Chiman is a Blogger & Reporter at AlyChiTech.com which covers a wide variety of topics from local news from digital world fashion and beauty . AlyChiTech covers the top notch content from the around the world covering a wide variety of topics. Aly is currently studying BS Mass Communication at University.