Jump to content

var text="";


Fangorn
 Share

Recommended Posts

Hi,

 

I just started to study javascript and I came across in one of your page to the following notation:

 

var text="";

 

The page is from w3school tutorial: http://www.w3schools.com/js/tryit.asp?filename=tryjs_loop_for_ex

 

I do not understand what it means and why should be used in the loop of the above example.

I tried to define var text; in its place but the result is:

 

undefinedThe number is 0The number is 1The number is 2The number is 3The number is 4

 

The only difference is the world 'undefined' at the beginning of the first line.

 

Many thanks in advance for your help

Link to comment
Share on other sites

It defines the variable called text as a string.

 

It's a to "get the variable on the page" so to speak.

 

It is simply a way to initialize a variable.

Link to comment
Share on other sites

Thanks niche,

 

so I suppose is always fundamental to define a variable as a string or as a number (var number = 0 or var text="").

 

I still have to understand how the loop works and the importance of the variable 'text' for the code itself.

I tried to delete the '+' from the code, and use text = ... instead of: text += "The number is " + i + "<br>";

and the result is only the last line, 'The number is 4'.

 

So I do not understand what text = text + "The number is " + i + "<br>"; means inside the loop, if one should explain it in words.

Taken for granted that understand "The number is " + i + "<br>", I suppose is just the code text = text + ... that prints out all the results of the loop. What happens there?

 

For instance, for i=0 we should have: text = text + The number is 0...

So what does 'text' represent?

 

I am sorry if the question looks so banal, but I am really stuck in this loop :(

Edited by Fangorn
Link to comment
Share on other sites

function myFunction() {    var text = ""; // declare the var and initialize the var to an empty string    var i; // declare the var    for (i = 0 ; i < 5 ; i++) {// loop from i=0 to i=4        text += "The number is " + i + "<br>"; // append a string to the string ending with a newline    }    document.getElementById("demo").innerHTML = text;// print the string to the screen inside the demo block}
  • Like 1
Link to comment
Share on other sites

text is a string.

 

When you do +=, or text = text + "string", what you're doing is making text have the same value it did before, but with something extra added onto it.

 

text = "A" means that text will become "A".

text += "A" means that text will become the current value of text followed by "A".

 

In the loop, if you use text = "The number is " + i + "<br>" then text will keep changing its value and will end up having the last value it was given. All a loop does is run the same code again multiple times.

  • Like 2
Link to comment
Share on other sites

Got it!!!

 

Therefore, using the code above, it will be:

for i = 0: text = "The number is 0" + <br>

for i = 1: text = text + "The number is 1" that is: text = "The number is 0" + <br> + "The number is 1" + <br>

for i = 2: text = text + "The number is 2" that is: text ="The number is 0" + <br> + "The number is 1" + <br> + "the number is 2" + <br>

... etc ...

 

Thank you very much!!! :) :) :)

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...