Jump to content

Variable Query


Krupa
 Share

Recommended Posts

I'm sort of new to JavaScript and I just saw an interesting utilization of variables. First off, I saw three different methods of declaring variables and I would just like to know how accurate I am with what they actually do.

var myVar = "" //This is a global variable that holds it's value throughout the script.myVar = "" //If a variable is declared like this, it's value is subject to change, just as I changed the value of the global myVar to hold this value.myVar; //This is reserving the variable (assigning it a null value) to be utilized later in the script when it's needed.

This is something along the lines of what I saw

<script type="text/javascript">x = "value_of_x";c = x.length;y = null;while(c--){	if(conditional){		y = x[c].innerHTML;		y = y.replace("","");	}}</script>

That's not the exact code, but more or less the idea and structure of it and I'm wondering what the purpose of assigning null to y and then replacing y's value with a value that currently exists in another variable is. Why not just leave it at x[c] rather than making y, which held a null value write itself over with a literal that already exists.Thanks :)

Link to comment
Share on other sites

Guest FirefoxRocks

You don't have to assign null to variables, but you should initialize them first:

var x = "value_of_x", c = x.length, y;while(c--){	if(conditional){		y = x[c].innerHTML;		y = y.replace("","");	}}

But I'm not sure if that code makes sense..., the if part.

Link to comment
Share on other sites

The if statement in the actual code makes sense when you know what the code is for :) I couldn't post the actual code so I had to give the general structure of it.Were my denotations of the variable types' functions correct?And thank you Firefoxrocks =)

Link to comment
Share on other sites

Were my denotations of the variable types' functions correct?
Not really.Any variable declared outside a function is global. It really doesn't matter if such a variable is declared with or without the var keyword. Global variables in JavaScript are also static. That means that the memory allocated to them stays allocated to them for the life of the document. If the memory holds a value, then that value stays associated with the variable for the life of the document, or until it is changed.A variable declared inside a function WITHOUT the var keyword is likewise global, and everything I wrote above applies just the same.A variable declared inside a function WITH the var keyword is local and temporary. It is not available outside the function, and the memory allocated to it is de-allocated when the function is finished.The value of all variables is subject to change. There is no mechanism for declaring a constant in JavaScript. (But of course you can use a variable as a constant if you like. A programming convention for doing so, borrowed from the C language, is to name such a variable in all CAPS.)Initializing a variable to a null value is one way that programmers can remind themselves what a variable is for. Having a global variable appear for the first time in the middle of a function is perfectly legal, but potentially confusing when you try to debug or maintain the function.
replacing y's value with a value that currently exists in another variable is. Why not just leave it at x[c] rather than making y, which held a null value write itself over with a literal that already exists.
In the somewhat tortured example you provided, it seems the function wants to copy/modify a value that exists in the document itself. (That's what innerHTML means.) Modifying that value without copying it would change the appearance of the document. You might not want to do that.Example. The caption of an image might read "Birds of a feather". Somewhere on your server you might have a file called "birds_of_a_feather.jpg" . That's pretty ugly for a caption, so you don't use it as one. But you can systematically recreate the filename from the reader-friendly caption by changing all text to lower case, replacing all spaces with underscores, and appending the dot and suffix. You don't change the caption itself, but changing a copy of it could be useful. Edited by Deirdre's Dad
Link to comment
Share on other sites

Just to add to that, the single way of declaring a variable in Javascript is using the var keyword. If you're not using var, you're not declaring a variable. You can assign a variable without declaring it first, but assigning isn't the same as declaring. If you declare a variable outside of a function it's global, if you declare it inside a function it's local. Just writing a variable name without using var is a no-op statement, it doesn't do anything at all.

Link to comment
Share on other sites

Thanks Deirdre's Dad and justsomeguy. That cleared quite a bit up for me :)Just to recap and ensure I understood everything correctly.

  • When you declare a variable outside of a function using the var keyword, the variable is global.
  • When you declare a variable inside a function, it's local to the function but acts globally within the function.
  • When you declare a variable outside of a function without using the var keyword, the variable is still global and not local to the function only. It is also no different from using the keyword, it's just a sloppy declaration.
  • Assigning a null value, or simply leaving the variable declared like so: var myVar; is just a reminder for programmers when dealing with a variable they may need later on.

Link to comment
Share on other sites

Pretty much. Doing this:var myVar;is fine, you should do that with all of your variables. That just declares the variable. When you use a variable without declaring it first, like this variable inside the function:

var x;function test(){  alert(x);}

Inside the function x hasn't been declared. So when you use x like that, it's just going to search for a declared variable. It will look inside the local scope first and, since it doesn't find a declared variable there, it's going to go up to the next scope (global scope) and check there, where it will find the declared variable. So if it can't find a variable in the current scope it just keeps going up a scope until it finds it. If it gets to the global scope and still hasn't found it, it just makes a new global variable.There's more than just 2 scopes though, you can chain your functions and each function will have its own scope. The local scope for each function becomes the global scope for functions declared inside of it.

var x = 'x';var y = 'y';var run_now = function(){  alert(x); // shows 'x'  var nested_func = function()  {	  var x = 'z';	  var other_func = function()	  {		var z = '123';		alert(x); // shows 'z'		alert(y); // shows 'y'	  }();	  alert(z); // error, z is undefined  }();  alert(x); // shows 'x'}(); // execute immediately

Edited by justsomeguy
Link to comment
Share on other sites

That was explained very clearly. Thank you very much justsomeguy, I enjoyed reading through that. :)Just to close this query, whenever I'm using variables that will be utilized inside functions, I should declare them ( var myVar; ) like so outside of the function(s) itself so there are no scope issues?

Edited by Krupa
Link to comment
Share on other sites

Guest FirefoxRocks

Always declare variables with var. Now I can also get into use 1 var statement per function, avoid global variables, etc, etc but let's leave that for another topic.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...