Posted on: Written by: K-Sato
⚠️ This article was posted over a year go. The information might be outdated. ⚠️

Table of Contents

What an exclamation mark means in JS/TS

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

That means if var is either null, false or undefined, the result of !var would be true. And if var is something else, the result of !var is false.

Here is an example.

When the variable is not null, false or undefined, it does not execute the code in the condition.

var user = { name: 'K-Sato', age: 23 }

if (!user) {
  console.log('Not executed')
}

But if the variable is null, false or undefined, the code in the condition gets executed.

var user = null

if (!user) {
  console.log('Executed')
}

Resources

About the author

I am a web-developer based somewhere on earth. I primarily code in TypeScript, Go and Ruby at work. React, RoR and Gin are my go-to Frameworks.