Hi @kusha.kpr!
Jumping right into your question about safely checking properties or methods on objects in JavaScript. Ah, this is a classic case that many new (and even experienced!) JS developers encounter!
The issue here is likely that me is undefined when you try to check me.onChange, so even typeof me.onChange throws an error because you’re essentially trying to access a property on an undefined value (undefined.onChange).
The best way to handle this is to first check that me itself exists before attempting to check its method:
JavaScriptif (me && typeof me.onChange === 'function') {
me.onChange(str);
}
This two-part check (me && ...) ensures me isn’t null or undefined before trying to access a method on it. You actually don’t need a try/catch block for this specific scenario!
Hope you find this helpful! ![]()