JavaScript 中的感叹号是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8012003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is an exclamation point in JavaScript?
提问by tmartin314
What does an exclamation mark before a function do?
函数前的感叹号有什么作用?
Example:
例子:
return !loadDynamicBlock();
回答by Dave Newton
A !
negates an expression.
A!
否定表达式。
In your example, if loadDynamicBlock()
returned true, the function calling it would return false, and vice-versa: !true == false
在您的示例中,如果loadDynamicBlock()
返回 true,调用它的函数将返回 false,反之亦然:!true == false
It can also be used to create actual booleans from JavaScript's ideas of truthy and falsy.
它还可以用于根据 JavaScript 的truthy 和 falsy思想创建实际的布尔值。
var a = 5;
!!(a - 5) === false;
!!(a + 5) === true;
回答by JaredPar
The !
in Javascript inverts a boolean expression.
在!
Javascript中反转布尔表达式。