What does the double exclamation marks (!!) means in Javascript?
As we all know, JavaScript is not a static language, but a dynamic language. That means a variable can reference or hold a value of any type and the type can be changed at any point. To better understand the double exclamation marks purpose let's consider you want to convert to a boolean the presence of a value:
const user = 'Doru';
console.log(typeof user); // => string
But we want a boolean, therefore a true/false only. Not truthy, but true. Not falsey, but false! We can sort that with a function:
function isUserPresent(user) {
if(user) {
return true;
}
return false;
}
isUserPresent('Doru'); // => true
isUserPresent(''); // => false
isUserPresent(0); // => false
Neat! But that is way too bulky. Let's use the !! magic:
const user = 'Doru';
console.log(!!user); // => true
const anotherUser = null;
console.log(!!anotherUser); // => false
Simple, concise, and effective.