0

I have a statement in my code:

if(!(typeof options.duration[i] === 'undefined'))

I have written it correctly, seems that there is no mistake but the console is throwing error that:

TypeError: options.duration is undefined

It should not show this error.It does not make any sense.

3 Answers 3

3

The variable options.duration is undefined, so accessing item i from it will result in this error. Perhaps try:

if(typeof options.duration !== 'undefined')

Or if you need to check both options.duration and options.duration[i], try

if(typeof options.duration !== 'undefined' &&
   typeof options.duration[i] !== 'undefined')
2
  • Thanks it worked. Plus, I think you should add () around both conditions. Commented Mar 13, 2014 at 16:31
  • @MuhammedTalhaAkbar That's not necessary, but if you find it makes the code more readable, then yes, you can put the parentheses around each condition.
    – p.s.w.g
    Commented Mar 13, 2014 at 16:35
1

For your test to succeed, the array options.duration must itself also be defined.

2
  • And options as well
    – Johan
    Commented Mar 13, 2014 at 16:28
  • @Johan it is not necessary because it is surely defined. ;) Commented Mar 13, 2014 at 16:32
1

You get that error because the duration property doesn't exist.

Check if the property exists before you try to check items in it:

if('duration' in options && typeof options.duration[i] !== 'undefined')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.