What is the !! (not not) operator in JavaScript?
2014-08-22 00:28
381 查看
http://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript
Question:
I saw some code that seems to use an operator I don't recognize, in the form of two exclamation points, like so:
Can someone please tell me what this operator does?
The context in which I saw this was,
Answer:
Coerces oObject to boolean. If it was falsey (e.g. 0,
etc.), it will be
otherwise,
So !! is not an operator, it's just the ! operator twice.
It converts a nonboolean to
an inverted boolean (for instance, !5 would be false, since 5 is a non-false value in JS), then boolean-inverts that so you get the original value as a boolean (so !!5 would be true).
Question:
I saw some code that seems to use an operator I don't recognize, in the form of two exclamation points, like so:
!!.
Can someone please tell me what this operator does?
The context in which I saw this was,
this.vertical = vertical !== undefined ? !!vertical : this.vertical;
Answer:
Coerces oObject to boolean. If it was falsey (e.g. 0,
null,
undefined,
etc.), it will be
false,
otherwise,
true.
!oObject //Inverted boolean !!oObject //Non inverted boolean so true boolean representation
So !! is not an operator, it's just the ! operator twice.
It converts a nonboolean to
an inverted boolean (for instance, !5 would be false, since 5 is a non-false value in JS), then boolean-inverts that so you get the original value as a boolean (so !!5 would be true).
相关文章推荐
- What is the $ symbol used for in JavaScript
- What is the Execution Context & Stack in JavaScript? -- To understand scope chain
- What is the Execution Context & Stack in JavaScript? [转载]
- What is the Execution Context & Stack in JavaScript? [转载]
- What is the difference between a function expression vs declaration in JavaScript?
- What is the difference between the ways to implement inheritance in javascript.
- (NOT CLEAR) Question 14: What is the correct declaration for a file stream insertion operator for a class my_stuff::my_class a
- what is the fashion IDE in Linux CPP programming?
- To see what is in the MAT-file prior to loading it, use whos -file
- What is the difference between _tmain() and main() in C++?
- [zt]---What is the data in the VSTS load test results store
- Q:Is there any way to define what the "Open Resource" dialog in Eclipse should show?
- What the hell is cmp byte ptr[rax],0 in a managed mini dump?
- what is the important things in my life?
- What is the difference between BIT and TINYINT in MySQL
- what is the fashion IDE in Linux CPP programming?
- Issue 12(Anyone can make things bigger and more complex. What require more effort and courage is to move in the opposite directi
- What do you think is the role of parents in preventing crime amongst young people?
- What Column Number Is It in the EXCEL?
- macro与inline的区别 What is the difference between macro and inline?