[thelist] Java Script and variables

James Aylard webmaster at equilon-mrc.com
Wed Jun 13 10:11:38 CDT 2001


Oliver,

> Is there a way for the same vaiable to be defined inside and outside a
> function?
...
> function getNum
> {
>     whatever = num1;
>     alert(num1);
> }
>
> alert(num1)

    Yes, but your example shows some confusion, so I'm not entirely clear
what it is you're asking. To answer the question most literally, you can do
this:

<script type="text/javascript">
   <!--
      var whatever = "foo" ;

      function getNum() {
         var whatever = "bar" ;
         alert(whatever)
      }

      alert(whatever) ;
   // -->
</script>

    Doing this makes the "whatever" variable declared inside of the function
separate from the one declared outside the function, however. It is
generally *not* advisable to use the same name for both a function-level
variable and a page-level variable because it can be very confusing to
debug.
    If that doesn't answer your question, please clarify and someone on the
list will be sure to help out.

James Aylard





More information about the thelist mailing list