Junior — Middle
What is the purpose of the '!!' operator in JavaScript and what does it do to an expression?
sobes.tech AI
Answer from AI
The !! operator in JavaScript is used to convert any value to a boolean type (true or false).
The first ! converts the value to a boolean and inverts it, and the second ! inverts it back, but as a boolean value.
Example:
console.log(!!0); // false
console.log(!!'text'); // true
console.log(!!null); // false
console.log(!!{}); // true
This is useful when you need to explicitly obtain a boolean value from any expression.