13

What does an exclamation mark before a function do?

Example:

return !loadDynamicBlock();
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
tmartin314
  • 4,061
  • 10
  • 39
  • 60

2 Answers2

30

A ! negates an expression.

In your example, if loadDynamicBlock() returned true, the function calling it would return false, and vice-versa: !true == false

It can also be used to create actual booleans from JavaScript's ideas of truthy and falsy.

var a = 5;
!!(a - 5) === false;
!!(a + 5) === true;
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
8

The ! in Javascript inverts a boolean expression.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454