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>
5
  • The console shows: Uncaught SyntaxError: Unexpected token ;. Stackoverflow is not a debugging service. Commented Dec 22, 2012 at 17:40
  • @FelixKling:Where did you get this error? Commented Dec 22, 2012 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. Commented Dec 22, 2012 at 17:43
  • @FelixKling: Thanks for the help.I am a beginner in web technology. Commented Dec 22, 2012 at 17:45
  • Also have a look at this question: stackoverflow.com/questions/1739221/… Commented Dec 22, 2012 at 17:47

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

3 Comments

What do you suggest for learning JavaScript?
You could read "JavaScript: The Definitive Guide" by David Flanagan. And "JavaScript: The Good Parts" by Douglas Crockford
0

You need to quote the ; parameter in this line

var jwords=words.join(';')

Comments