K
K
kr_ilya2020-06-28 11:20:04
JavaScript
kr_ilya, 2020-06-28 11:20:04

How to check for Undefined?

How to make a check for undefined so that the program is not cut off with a warning?

Tried to do so

var o = {
        	action: getRandomItems
        }
if(typeof o.children !== undefined){

}


So
var o = {
        	action: getRandomItems
        }
if(!!o.children){

}


In any case, I get
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'children' of undefined

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2020-06-28
@kr_ilya

Learn to read mistakes. It says here that undefinedthe object you are trying to read childrenfrom, and not yourself children. In your examples (which, of course, work without problems) in its place is o.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question