What is the difference between angular.isUndefined (value) and not! (value)?

I tried:

if(angular.isUndefined(value)){ // something } 

and

 if(!(value)){ // something } 
  • Is there a difference between the two?
  • Is there a use case instead of another?
+5
source share
2 answers
 var foo = false; if(!foo) { // will log console.log("foo is defined but false"); } if(angular.isUndefined(foo)){ // will not log as foo is defined console.log("foo is undefined") } 

another example without defining foo

 if(!foo) { // will throw exception "Uncaught ReferenceError: foo is not defined " console.log("foo is defined but false"); } if(angular.isUndefined(foo)){ // will log console.log("foo is undefined") } 

angular is so effective. isUndefined (foo) does nothing but evaluate

 if(typeof foo == "undefined") 

wrapped to save 1 character yes.

while! -operator checks if the given variable matches false therefore

 if(!foo) 

coincides with

 if( foo != true) 

UPDATE:

As stated in the comments, when I write "evaluates to false", there is false null undefined NaN "" (empty string) and 0 are included

+2
source

! is not a logical operator in JavaScript, but angular.isUndefined (value) checks if the link matches undefined

Which one to use depends entirely on what you are trying to do at the end.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators and https://docs.angularjs.org/api/ng/function/angular.isUndefined

0
source

Source: https://habr.com/ru/post/1201652/


All Articles