Programming Tips - What's the best way to check if a javaScript member variable is defined?

Date: 2009apr24 Language: javaScript Keywords: is_defined, isDefined Q. What's the best way to check if a javaScript member variable is defined? A. Use triple equals (exactly equals) operator with "undefined" like this:
<script> const a = [1, 2, 3]; if (a['hello'] === undefined) { alert('UNdefined'); } else { alert('DEFINED'); } </script>
Or you can make a function:
<script> function isArrayElementUndefined(a, element) { return a[element] === undefined; } function isArrayElementDefined(a, element) { return !isArrayElementUndefined(a, element); } function exampleUse() { const a = [1, 2, 3]; if (isArrayElementUndefined(a, 'hello')) { alert('UNdefined'); } if (isArrayElementDefined(a, 'hello')) { alert('DEFINED'); } } </script>