hokeyplyr48
hokeyplyr48

Reputation: 95

JavaScript : Check if variable exists and if equal to value

I have three pages utilizing the same code and on one of the pages this variable doesn't exist, on the other two the variable ticketType has a value of 1 or 2. I need to first check if ticketType exists and isn't undefined and secondly, need to determine if it's one or 2.

This if statement generates an error:

if(typeof ticketType != undefined && ticketType == 1){}

It's saying ticketType isn't defined. I tried nesting the if statements to check if it was defined first thinking it wouldn't go and try the inner if statement but firebug still generates an error.

Any ideas? There has to be a way to do this...

Upvotes: 7

Views: 32368

Answers (8)

Faris Rayhan
Faris Rayhan

Reputation: 4636

simlpe using console.log(someVar);

Upvotes: -3

amit_g
amit_g

Reputation: 31250

undefined should be within quotes...

if (typeof ticketType !== "undefined" && ticketType == 1)
{
}

EDIT

Here we are not talking about global.undefined which doesn't have to be enclosed within quotes. We are talking about the return type of typeof operator which is a string. Incidentally for undefined variable, the typeof returns "undefined" and thus we need to enclose it within string.

// ticketType is not defined yet

(typeof ticketType !== undefined) // This is true
(typeof ticketType === undefined) // This is false
(typeof ticketType !== "undefined") // This is false
(typeof ticketType === "undefined") // This is true

var ticketType = "someValue"; // ticketType is defined

(typeof ticketType !== undefined) // This is still true
(typeof ticketType === undefined) // This is still false
(typeof ticketType !== "undefined") // This is true
(typeof ticketType === "undefined") // This is false

So the correct check is against "undefined" not against global.undefined.

Upvotes: 3

Jonathan M
Jonathan M

Reputation: 17451

The correct syntax is:

if (typeof ticketType !== 'undefined' && ticketType === 1) { }

The result of the typeof operator is always a string. See here: http://www.javascriptkit.com/javatutors/determinevar2.shtml

Upvotes: 0

zzzzBov
zzzzBov

Reputation: 179046

you're incorrectly checking for an undefined variable, but if you're planning on using it, why not just make sure that it gets defined?

ticketType = ticketType || 1;
if (ticketType === 1) {
  //do stuff
}

As everyone else has shown, the standard way of checking for undefined values in JS is:

typeof somevar === 'undefined'

Upvotes: 0

Kai Qing
Kai Qing

Reputation: 18833

if(typeof ticketType != 'undefined' && ticketType == 1){}

I think you need the quotes around the undefined word. At least I always do it that way.

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

The error message is pretty clear. Try with this:

var ticketType;
if(typeof ticketType != undefined && ticketType == 1){}

You cannot just reference a variable that does not exist. You can only do this when assigning:

ticketType = 1;

The browser complaints because ticketType is an unknown identifier. However if we first declare it (even without assigning it any value) it becomes known but with undefined value.

Upvotes: 0

T. Stone
T. Stone

Reputation: 19495

Wrap it in parenthesis.

if (typeof(ticketType) !== 'undefined' && ticketType === 1)

Upvotes: 0

locrizak
locrizak

Reputation: 12281

'undefined' needs to have quotes around it when used with typeof

if(typeof ticketType != 'undefined' && ticketType == 1){}

Upvotes: 12

Related Questions