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

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

##### 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
##### 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}`
• 1
##### 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.

• 2
##### 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!!! :)
{"ft_lang_label":"__label__en","ft_lang_prob":0.91827196,"math_prob":0.964754,"size":959,"snap":"2021-31-2021-39","text_gpt3_token_len":234,"char_repetition_ratio":0.12460733,"word_repetition_ratio":0.0,"special_character_ratio":0.23357664,"punctuation_ratio":0.11,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98906505,"pos_list":[0,1,2,3,4,5,6,7,8,9,10],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-27T21:38:28Z\",\"WARC-Record-ID\":\"<urn:uuid:edd21956-1147-42bd-88ac-a88f0072388e>\",\"Content-Length\":\"121802\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:cac08d36-e5a5-44f2-9010-567d91f0ec99>\",\"WARC-Concurrent-To\":\"<urn:uuid:1dd31fb2-4e42-40df-aeed-29f2282fcd81>\",\"WARC-IP-Address\":\"13.249.38.19\",\"WARC-Target-URI\":\"https://w3schools.invisionzone.com/topic/50944-var-text/?tab=comments#comment-281129\",\"WARC-Payload-Digest\":\"sha1:HGLGQUHGXQZJYGXVUJQ4Y7FAVJ4RJNRP\",\"WARC-Block-Digest\":\"sha1:D4OCRJJZJSYW6JJHL2K5MJQDZT6QC2BO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780058552.54_warc_CC-MAIN-20210927211955-20210928001955-00424.warc.gz\"}"}