• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

uninitialized default global variable

 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When gva is called, there is an error, and the problem is the variable scd. I assumed that scd would be initialized to an empty array when the page loads, but it doesn't seem to happen that way.
The variable scd is not initialized to anything in any function, but I assumed that scd would get the default value of an empty array.
I want to initialize scd only once, so I don't want to put the initialization in any function, because functions get called more than once.
When a variable is not initialized yet, what is it? A null? A default? A what?
And how can I check with code if it's initialized or not?
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If line 01 is at the top level (not within a function) it will create and initialize a global variable named scd. (Not the best name choice, in my opinion.)

If the function is called after this initialization takes place, there should be no error.

You say "there is an error". Keeping the nature of the error a secret doesn't help us help you. (Please read ItDoesntWorkIsUseless.)

Where/when is the function gva() (please consider better naming) called? If it's before the initialization, yeah, there will be a problem.
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


HTML is:
<INPUT TYPE="button" VALUE="Check" onclick="check()">


<TEXTAREA ROWS="10" ID="progress" COLS="50"></TEXTAREA>

I've passed Arrays, strings, integers to efgh(newStuff) and the result is always that what I pass gets displayed in the textarea called "progress". But in this case, nothing is displayed and there is a message at the bottom of the browser window that an error has happened, and also lines which follow the "problem line" efgh(scd); are not executed.
 
Bear Bibeault
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the exact nature of the error message. And you code still doesn't show how the code in question is being called.
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I made a whole file which contained only the code lines which are relevant. Then I got no error message. So I realized that the problem must be caused somewhere else in the program. I looked at it closely and just above the line
scd=new Array();
there was this line:
rqp;
I thought that might be the problem. In java, you can declare field variables without initializing them, so I thought you could do that in javascript. Guess not. I tried changing
rqp;
to
rqp=0;
and now everything works fine.
What I think is that when the computer read the program, it stopped at the rqp line.
I'm just wondering, when does the browser read the global variable assignments? Is it when the page is loaded, or does the browser read the global variables only when the program attempts to reference a variable which does not yet have a value?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic