Checking whether a variable is set in javascript -


i want js function says whether variable set. i've defined isset() below.

it behaves strangely described in comments. ideas?

<html>     <head></head>     <body>         <script type="text/javascript">            function isset(variablename) {             return typeof variablename != 'undefined';           }            = 5;           console.log(isset(a));            console.log(isset(b)); // returns error.         </script>     </body> </html> 

you need perform typeof check directly, not inside function.

the reason same 1 in php (where function break unless used pass-by-reference):

isset(b) results in exception being thrown because b undefined variable , passing argument tries read cannot do. typef check never executed.


Comments