what is the real meaning of using (!)before the scope variable in angularjs. ng-show="!!file". What would indicate if I use this symbol in angularjs ng-directives
-
It is the way to convert whatever to boolean value – Slava Utesinov Nov 16 '17 at 05:59
-
okay. can i use single ! symbol – N15 Nov 16 '17 at 06:03
-
Of course, you can – Slava Utesinov Nov 16 '17 at 06:04
-
you saved my time. simple and concise answer i got from u @SlavaUtesinov – N15 Nov 16 '17 at 06:12
4 Answers
From: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
In JavaScript, a truthy value is a value that is considered true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN).
The ! is just a "not", but it has the effect of changing any truthy value into a false boolean and any falsy value into a true boolean. When you bang-bang a variable, you are basically converting it into a true real boolean value based on it's truthy/falsy behavior. This is generally a good practice but isn't entirely necessary.

- 107
- 10
-
Woops, I meant a "real" boolean variable as opposed to implied. I updated the answer to reflect that. – Andrew Nov 16 '17 at 06:29
!!
is used as a shorthand for converting values to an explicit boolean value.
If file
is "truthy", !!file
will be true
. If it's "falsy", !!file
will be false
.
var nonEmptyString = "nonempty string";
var emptyString = "";
console.log(!!nonEmptyString);
console.log(!!emptyString);
The use of !!
in your example is extraneous and pointless because ng-show
will take care of checking whether the value is "truthy" or "falsy".

- 99,490
- 19
- 131
- 169
var nonEmptyString = "nonempty string";
var emptyString = "";
console.log(!!nonEmptyString);
console.log(!!emptyString);

- 305
- 2
- 4
- 17