25

I'm trying to learn JavaScript by going through some code in an application and I keep seeing !variable in if conditions. For example:

if (!variable.onsubmit || (variable.onsubmit() != false)) {

What is it? Some kind of test if the variable is empty?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Winters
  • 980
  • 3
  • 11
  • 16
  • 2
    Just a note: if you are searching for an operator on a search engine, use the full name for it (for example "exclamation mark javascript" if you want to search for the meaning of `!`). – Qantas 94 Heavy Oct 21 '13 at 10:09
  • 7
    People closing this is "lack of basic understanding" - how do you figure that? What _previous_ knowledge of JavaScript do you expect Winters to have when approaching this problem? This is an absolute basic. Just because a problem seems easy doesn't mean it's a bad question. This is widely applicable. If anyone has anything to add please edit my answer (it's community wiki). – Benjamin Gruenbaum Oct 21 '13 at 10:16
  • Thanks @Qantas94Heavy for the tip on searching for these terms. Just thought I'd add I did search for it quite a bit but I expected an argument as per w3schools example !(x==y) which threw me off. – Winters Oct 21 '13 at 10:31
  • 2
    @Winters generally speaking. W3schools is [not a very reliable source](http://www.w3fools.com). Prefer MDN or Stack Overflow when you are able (the spec is of course always the best but like Qantas94Heavy mentioned it is also hard to read at first). – Benjamin Gruenbaum Oct 21 '13 at 11:17

4 Answers4

57

! is the logical not operator in JavaScript.

Formally

!expression is read as:

  • Take expression and evaluate it. In your case that's variable.onsubmit
  • Case the result of that evaluation and convert it to a boolean. In your case since onsubmit is likely a function, it means - if the function is null or undefined - return false, otherwise return true.
  • If that evaluation is true, return false. Otherwise return true.

In your case

In your case !variable.onsubmit means return true if there isn't a function defined (and thus is falsy), otherwise return false (since there is a function defined).

Simply put - !variable means take the truth value of variable and negate it.

Thus, if (!variable) { will enter the if clause if variable is false (or coerces to false)

In total

if (!variable.onsubmit || (variable.onsubmit() != false)) {

Means - check if variable.onsubmit is defined and truthy (thus true), then it checks if calling onsubmit returns a result that coerces to true. In a short line it checks if there is no onsubmit or it returns true.

Next time, how do I find this myself?

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Ok, I kind of get it now, but why is the second part of the OR checking if variable.onsubmit() != false? Why not check if variable.onsubmit() = true? – Winters Oct 21 '13 at 10:16
  • @Winters or just `variable.onsubmit()`. There is no reason to do that. No way to say it nicely - the second part of that statement is just badly coded. – Benjamin Gruenbaum Oct 21 '13 at 10:18
  • I am not surprised based on the rest of this code, thank you. – Winters Oct 21 '13 at 10:21
7

It is a negation operator used for truth tests on a variable.

var myVariable = 1;

if ( ! myVariable )
{
    // myVariable evaluates as false
}

if ( myVariable )
{
    // myVariable evaluates as true
}
David Barker
  • 14,484
  • 3
  • 48
  • 77
2

The selected answer already answers the question. One thing to add in this is that ! operator can be used in a rather interesting fashion.

obj = {}
if (!!obj) {console.log('works')} // !!obj = true
obj = undefined
if (!!obj) {console.log('does not work')} // !!obj = false

So double !! will change any expression to a boolean value with no exceptions.

This has a very peculiar use case in some cases.

  • Lets say there is a function that returns either true or false and nothing else. In that case one could just change return returnValue to return !!returnValue for extra caution (although not need in most of the cases)

  • Conversely, if a function only accepts true or false and nothing else, one could just call the function as functionA(!!parameter) instead of functionA(parameter), which again is not needed in most of the cases, but could ensure extra security

Prasanna
  • 4,125
  • 18
  • 41
0

! is a logic reversal operator, if something was true it will change it to false, if something is false, it will change to true.

example, we know that empty string or 0 in boolean is false.

let a = Boolean("")
console.log(a) // shows false, because empty string in boolean is false
console.log(!a) // shows true, because logic is reversed

easier example:

let a = 5
let b = 1
let c = a > b
console.log(c) // shows true, since a,5 is bigger than b,1
console.log(!c) // shows false, since logic is now reversed
Pedro Pinheiro
  • 1,059
  • 14
  • 32
fruitloaf
  • 1,628
  • 15
  • 10