Javascript: define a variable if it doesn’t exist

javascriptvariables

i feel like im trying to do something super simple, but just being stupid about it.

all i want to do is see if a variable has been set previously, and if it has NOT, set it with a default value….here is a sample:

if(!embed_BackgroundColor) {
    var embed_BackgroundColor;
    embed_BackgroundColor = "#F4F4F4";
}

so, once you stop laughing at my code….WHY is it overwriting the variable no matter what?

please save my nerves;)

Best Solution

Pro style:

var SomeVar = SomeVar || 'Default Value';