0

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

N15
  • 305
  • 2
  • 4
  • 17

4 Answers4

1

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.

Andrew
  • 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
1

!! 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".

JLRishe
  • 99,490
  • 19
  • 131
  • 169
0

! is for reverse Boolean and !! double reverse Boolean, Please check here: Click

D. Pareek
  • 709
  • 4
  • 12
0

var nonEmptyString = "nonempty string";
var emptyString = "";

console.log(!!nonEmptyString);
console.log(!!emptyString);
N15
  • 305
  • 2
  • 4
  • 17