Blagoh
Blagoh

Reputation: 1235

Define global if it doesn't exist

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.

Upvotes: 0

Views: 56

Answers (2)

Daryl Ginn
Daryl Ginn

Reputation: 1424

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';
}

Upvotes: 2

ateich
ateich

Reputation: 520

In the global scope add:

var raw;

Upvotes: 1

Related Questions