0

I'm trying to set a global if it doesnt exist. Total noob question.

I'm doing this:

if (!raw) { raw = 'yep'; } 

But it keeps throwing reference error raw is not defined.

2
  • If you're getting a reference error then you are likely running in strict mode. Commented Dec 19, 2014 at 1:14
  • 2
    nope, he gets reference errors cause he doesn't "reference" raw before checking for it's value, if you wanna test for a vaiable if it exists you'd have to use "typeof raw == 'undefined'" or window.raw Commented Dec 19, 2014 at 2:01

2 Answers 2

2

This is one way, assuming window exists:

if(!window.raw) { raw = 'yup'; } 

If not, you could just make a global object to use:

globals = {}; if(!globals.raw) { globals.raw = 'yup'; } 
Sign up to request clarification or add additional context in comments.

Comments

1

In the global scope add:

var raw; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.