Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angularjs

I've tried:

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

and

if(!(value)){
    // something
}
  • Is there a difference between the two?
  • Is there a use-case to choose one instead of the other?
like image 255
zoomZoom Avatar asked Sep 02 '14 14:09

zoomZoom


People also ask

How to check undefined value in AngularJS?

The angular. isUndefined() function in AngularJS is used to determine the value inside isUndefined function is undefined or not. It returns true if the reference is undefined otherwise returns false. Return Value: It returns true if the value passed is undefined else returns false.

What is angular isDefined?

The angular. isDefined() function in AngularJS is used to determine the value inside isDefined function is defined or not. It returns true if the reference is defined otherwise returns false.


1 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 define 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")
}

so effective angular.isUndefined(foo) does nothing else than evaluating

if(typeof foo == "undefined")

wrapped for saving 1 character yeah.

while !-operator checks if a defined variable evaluates to false so

if(!foo) 

is the same like

if( foo != true)

UPDATE:

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

like image 70
john Smith Avatar answered Nov 01 '22 17:11

john Smith