What does the exclamation mark do before the function?
JavaScript syntax 101: here is a function declaration:
function foo() {}
Note that there’s no semicolon; this is just a function declaration. You would need an invocation, foo()
, to actually run the function.
Now, when we add the seemingly innocuous exclamation mark: !function foo() {}
it turns it into an expression. It is now a function expression.
The !
alone doesn’t invoke the function, of course, but we can now put ()
at the end: !function foo() {}()
, which has higher precedence than !
and instantly calls the function.
function foo() {}()
would be a syntax error because you can’t put arguments (()
) right after a function declaration.
So what the author is doing is saving a byte per function expression; a more readable way of writing it would be this:
(function(){})();
Lastly, !
makes the expression return a boolean based on the return value of the function. Usually, an immediately invoked function expression (IIFE) doesn’t explicitly return anything, so its return value will be undefined
, which leaves us with !undefined
which is true
. This boolean isn’t used.
What does an exclamation mark before a function really mean in PHP
The !
preceding the function is the same as...
if (stripos($haystack, $needle) == FALSE) {}
It's the same because it is a ==
comparison which doesn't check types.
It's called the negation unary operator. It flips the Boolean value (coercing to Boolean if need be) of a value.
For example...
! 0; // True
! 1 ; // False
! ''; // True
! true; // False
!! 0 // False
The !!
trick is handy in languages without a (bool)
cast. By flipping a value twice, you get the Boolean version of its original value.
What does an exclamation mark before a variable mean in JavaScript
!
is the logical not operator in JavaScript.
Formally
!expression
is read as:
- Take
expression
and evaluate it. In your case that'svariable.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?
- MDN has a list of operators here.
- The language specification specifies such operators, though being the official specification it does contain some jargon which might be hard to understand.
What does an exclamation mark (!) before a statement do?
Any command prepended by exclamation point is run by your operating system shell instead of python. jt
is actually a separate app called by your shell.
What does the exclamation mark mean before a function call?
"!"
is a new dart operator for conversion from a nullable to a non-nullable type.
Read here and here about sound null safety.
Use it only if you are absolutely sure that the value will never be null and do not confuse it with the conditional property access operator.
Why does an exclamation mark before a variable return 'true' if the variable value is zero?
! is known as the logical NOT operator. It reverses the boolean result of the operand (or condition)
0 is also considered as the boolean false, so when you use !variable you are using the logical operator and saying it to change the value of the variable to its opposite, that in boolean is true
0 == false == !1 == !true
1 == true == !0 == !false
in Javascript are considered false:
false, null, undefined, "", 0, NaN
are considered true:
true, 1, -0, "false". <- the last one is a not empty string, so its true
if( false || null || undefined || "" || 0 || NaN) //never enter
if( true && 1 && -1 && "false") //enter
https://developer.mozilla.org/en-US/docs/Glossary/Falsy
What does the ! (exclamation mark) mean before selector in jQuery
It negates a boolean.
Meaning:
true becomes false
and
false becomes true
In this example it means:
if(!$('selector').hasClass('something'))
if
selector
has not class something
The exclamation mark does not apply on the jQuery selector but on the result of the hasClass
function. It is like you have used brackets (but you don't have to use brackets, JavaScript knows that this is meant).
!( $('selector').hasClass('something') /* e.g. returns true */ ) /* now it's false */
In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
That's the non-null assertion operator. It is a way to tell the compiler "this expression cannot be null
or undefined
here, so don't complain about the possibility of it being null
or undefined
." Sometimes the type checker is unable to make that determination itself.
It is explained here:
A new
!
post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact. Specifically, the operationx!
produces a value of the type ofx
withnull
andundefined
excluded. Similar to type assertions of the forms<T>x
andx as T
, the!
non-null assertion operator is simply removed in the emitted JavaScript code.
I find the use of the term "assert" a bit misleading in that explanation. It is "assert" in the sense that the developer is asserting it, not in the sense that a test is going to be performed. The last line indeed indicates that it results in no JavaScript code being emitted.
What does an exclamation mark before a cell reference mean?
When entered as the reference of a Named range
, it refers to range on the sheet the named range is used on.
For example, create a named range MyName
refering to =SUM(!B1:!K1)
Place a formula on Sheet1
=MyName
. This will sum Sheet1!B1:K1
Now place the same formula (=MyName
) on Sheet2
. That formula will sum Sheet2!B1:K1
Note: (as pnuts commented) this and the regular SheetName!B1:K1
format are relative, so reference different cells as the =MyName
formula is entered into different cells.
Related Topics
Why Does a Regexp With Global Flag Give Wrong Results
Why Is Settimeout(Fn, 0) Sometimes Useful
Convert Date to Another Timezone in JavaScript
Difference Between a Function Call and Function Reference
How to Empty an Array in JavaScript
What Does the Construct X = X || Y Mean
Why Would a JavaScript Variable Start With a Dollar Sign
Escaping HTML Strings With Jquery
JavaScript - Sort Array Based on Another Array
Parsing a String to a Date in JavaScript
How to Format Numbers as Currency Strings
Window.Onload VS $(Document).Ready()
Is There an "Exists" Function For Jquery
How to Replace Plain Urls With Links