+ 2
How do I fix unexpected token ' { '
I was creating a function in JavaScript and the page wouldn't run the code but instead said there was an unexpected token. function myFunction { var x=document.getElementById("my law") document.getElementById("demo").innerHTML }
9 Réponses
+ 3
After "myFunction" you need parentheses as such:
function myFunction (){
/ / stuff
}
You need the parentheses even if you have no parameters. Hope I helped!
+ 2
I prefer not to leave out ; semicolons where experts recommend to use them even if the script works without them. For readability and for preventing introduction of bugs by mistake.
Here's a very nice article about code conventions for JavaScript by Douglas Crockford (JSON):
javascript.crockford.com/code.html
+ 1
thx for the article, I appreciate it, I'll look over it :D
0
And you missed the semicolon
0
In my experience, semicolons in javascript didn't matter.
0
@Cocoa But they're extremely recommended
0
I suppose, I'll credit you that, it'll work either way so whatever haha :D
0
@ Cocoa, will 'var x = 5' work in a browser that doesn't support ECMAscript6?
0
@ZinC Yes