7

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...

amit_g
  • 30,880
  • 8
  • 61
  • 118
hokeyplyr48
  • 95
  • 1
  • 2
  • 6

8 Answers8

12

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

if(typeof ticketType != 'undefined' && ticketType == 1){}
locrizak
  • 12,192
  • 12
  • 60
  • 80
3

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.

amit_g
  • 30,880
  • 8
  • 61
  • 118
  • I dont beleive undefined needs to be in quotes. My understanding is the only issue with undefined is that it can be accidentally overwritten. – Prisoner ZERO Nov 17 '11 at 21:58
  • Nice explanation, but as long as You are sure of comparing two strings (or two integers) then there is no need to check variable type by `===` or `!==`. `typeof` always return string, then `typeof ticketType != "undefined"` is enough – instead Sep 14 '16 at 23:43
0
if(typeof ticketType != 'undefined' && ticketType == 1){}

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

locrizak
  • 12,192
  • 12
  • 60
  • 80
Kai Qing
  • 18,793
  • 5
  • 39
  • 57
0

Wrap it in parenthesis.

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

T. Stone
  • 19,209
  • 15
  • 69
  • 97
0

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

Jonathan M
  • 17,145
  • 9
  • 58
  • 91
0

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.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
0

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'
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
-3

simlpe using console.log(someVar);

Faris Rayhan
  • 4,500
  • 1
  • 22
  • 19