-1

I'm trying to understand better how to determine if a variable is undefined and I get mixed results, hopfuly someone can make it clearer.

This is the example:

enter image description here

What I expect is none_exisiting_variable to result 'false' for the isDefined check.

Ben Diamant
  • 6,186
  • 4
  • 35
  • 50
  • Related: [JavaScript check if variable exists (is defined/initialized)](http://stackoverflow.com/questions/5113374/javascript-check-if-variable-exists-is-defined-initialized-which-method-is-b) – Jonathan Lonowski Jan 09 '15 at 14:24
  • Hey thanks, I came across this post but wanted to discover if there is an angular way to overcome this – Ben Diamant Jan 09 '15 at 14:25

2 Answers2

1

That's the difference between

  1. an undefined value, or a variable whose value is undefined (that's what can check angular.isDefined)
  2. an undefined variable : any reference to its name is a reference error

To check a variable is not defined, do

if (typeof myvariable === "undefined") {
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0
var foo = 'foo';
angular.isDefined(foo) // true;
angular.isDefined(bar) // false
var bar = 'bar';
Code Whisperer
  • 22,959
  • 20
  • 67
  • 85