JavaScript/TypeScript Tip: Understanding the Double Bang Operator

🚀 JavaScript/TypeScript Tip: The Double Bang (!!) Operator Ever seen code like this and wondered what’s going on? 👇 const isActive = !!user; Let’s break it down 👇 The double bang (!!) operator is a quick way to convert any value to a boolean in JavaScript or TypeScript. 💡 Here’s how it works: The first ! negates the value (turns truthy → false, falsy → true) The second ! negates it again (bringing it back to the correct boolean form) So instead of: const isActive = Boolean(user); you can write: const isActive = !!user; ⚡ When to use it: Use !! when you want to ensure a value is strictly true or false — for example, in conditions or when returning a clean boolean from a function.

To view or add a comment, sign in

Explore content categories