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

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -