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

0

8 Answers 8

12

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

if(typeof ticketType != 'undefined' && ticketType == 1){}
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

I dont beleive undefined needs to be in quotes. My understanding is the only issue with undefined is that it can be accidentally overwritten.
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
0

Wrap it in parenthesis.

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

2 Comments

typeof isn't a function. There isn't any need for parentheses.
i dont think typeof needs parens. confused me first time I used it.
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.

Comments

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.

Comments

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'

Comments

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

1 Comment

The linked resource is wrong, if the operand of typeof is null it will evaluate to the string "object", e.g.: typeof null == 'object'
-3

simlpe using console.log(someVar);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.