0

So here's what I've done so far:

<script>
text = new Array("t", "te", "tes", "test");
for(new i; i < text.length; i++)
{
    document.getElementById('text').innerHTML=text[i];
}
</script>


<body>
<span id='text'></span>
</body>

I'm going to add an timer later, but my question is, why do I get "ReferenceError 'i' is not defined"? It is defined as you can see.. Thanks in advance.

Bhushan
  • 6,017
  • 12
  • 54
  • 89
prk
  • 3,483
  • 6
  • 14
  • 23
  • 2
    You declare a variable using `var` keyword. [JavaScript 101](http://hsablonniere.github.io/markleft/prezas/javascript-101.html). – undefined Jan 22 '14 at 05:54
  • 3
    Side note: you should generally use [array literal notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Array_literals) and not `new Array()`. In your case, that would look like `var text = ['t', 'te', 'tes', 'test'];`. – Matt Ball Jan 22 '14 at 05:56
  • Matt, what does that matter? Just curious. – prk Jan 22 '14 at 05:56
  • See http://stackoverflow.com/q/931872/139010 and http://stackoverflow.com/q/1094723/139010 and http://stackoverflow.com/q/885156/139010 – Matt Ball Jan 22 '14 at 05:59

4 Answers4

7

Instead of:

for(new i; i < text.length; i++)

Try this:

for(var i=0; i < text.length; i++)

You are using new, and in javascript we have to use var for declaring variables.

Update1

As @MattBall and @oxfn has mentioned in comments, your script is running before the DOM is ready. So you have to place the script after body tag.(or you can you jquery).

Following is working code:

<body>
    <span id='text'></span>
    <script>
    var text = new Array("t", "te", "tes", "test");
    for(var i=0; i < text.length; i++)
    {
        document.getElementById('text').innerHTML = document.getElementById('text').innerHTML +":"+ text[i];
    }
    </script>
</body>
Bhushan
  • 6,017
  • 12
  • 54
  • 89
2

You can also use this method aswell,

    var text = new Array("t", "te", "tes", "test");
    text.forEach(function(textvalue) {
        console.log(textvalue);
        document.getElementById('text').innerHTML=textvalue;
    });

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

To display array content value into <div id="text"></div>

<script>
      function ArrayPrint(){
            var text = ["t", "te", "tes", "test"];
            text.forEach(function(textvalue) {
                console.log(textvalue);
                document.getElementById('text').innerHTML  += textvalue +' | ';
            });
      }
      window.onload = ArrayPrint;
</script>

HTML:

  <div id="text"></div>
Krish R
  • 21,556
  • 6
  • 47
  • 57
0
<!DOCTYPE html>
<html>
<body>
<span id='text'></span>
</body>
</html>


<script  type="text/javascript">
text = new Array("t", "te", "tes", "test");
for(var i=0; i < text.length; i++)
{
    document.getElementById('text').innerHTML=text[i];
}
</script>
Anubhav
  • 1,537
  • 3
  • 15
  • 30
0

As int in JavaScript is primitive type and it initialization not required the new keyword and JavaScript code working on DOM needs that DOM is already ready to use when code is running so place your script code at bottom just before close of body tag or call it on load.

Also you can cache length property it enhance performance.

    for(var i = 0,max=text.length; i < max; i++)
    {
        document.getElementById('text').innerHTML = document.getElementById('text').innerHTML +":"+ text[i];
    }
Zaheer Ahmed
  • 26,435
  • 11
  • 70
  • 105