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')
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it worked. Plus, I think you should add () around both conditions.
@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.
1

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

2 Comments

And options as well
@Johan it is not necessary because it is surely defined. ;)
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')

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.