What you do is valid, but it has flaws.
if (opt && opt.key && opt.key2) {
This check will fail if it opt.keyhas false values [0, null, false, etc.]
In this case, you will need to do a type check to make sure that it is not undefined.
if (opt && typeof opt.key !== "undefined" && opt.key2) {
source
share