0

Why is array push not working in the following code? Can someone find the mistake please ?

<html>
<body>
<script>
words=new Array("limit","lines","finish")
words.push("complete","In","Out")
var jwords=words.join(;)
document.write(jwords)
</script>
</body>
</html>
  • The console shows: `Uncaught SyntaxError: Unexpected token ;`. Stackoverflow is not a debugging service. – Felix Kling Dec 22 '12 at 17:40
  • @FelixKling:Where did you get this error? –  Dec 22 '12 at 17:42
  • As I said, in the console. Chrome has built in developer tools and so does Firefox. You can also use Firebug for Firefox and there should be some extensions for IE too. – Felix Kling Dec 22 '12 at 17:43
  • @FelixKling: Thanks for the help.I am a beginner in web technology. –  Dec 22 '12 at 17:45
  • Also have a look at this question: http://stackoverflow.com/questions/1739221/what-is-a-good-javascript-debugging-tool – Felix Kling Dec 22 '12 at 17:47

2 Answers2

2
words.join(;)

should be:

words.join(";");

This is in fact a syntax error and can be caught by your browser (F12).

Some tips:

  1. Create variable names with the keyword var. Your words variable was not created with var.

  2. Don't use the Array constructor. Use the array literal syntax []. That's means change the first line to:

    var words = [ 'limit', 'lines', 'finish' ];
    
  3. Use console.log instead of document.write. You can view the result in your browser by hitting F12. document.write causes problems when used in certain situations so it's best to avoid it.
0x499602D2
  • 87,005
  • 36
  • 149
  • 233
0

You need to quote the ; parameter in this line

var jwords=words.join(';')

Vimalnath
  • 6,202
  • 2
  • 24
  • 45