javascript - does it make sense to test whether undeclared variable was defined earlier in code? -
i'm studying javascript , have no programming background @ all. in example think understand foo undeclared (global) variable doesn't exist when bar calculated, bar = (undefined + 1) or nan.
var bar = foo + 1, foo = 1;
let's toward end of long bunch of code. if foo had been defined , foo = 1 intended re-set value of foo? make sense test it? think i'm opening scope can of worms.... ("scope? can't haaaandle scope!")
i thank in advance patience learning curve! answers phrase in analogy or metaphor under belt....
you can test , declare of not declared using or operator ||
foo = foo || 1;
so if foo exists, foo foo. if foo not exist, foo 1.
Comments
Post a Comment