+ 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 }

12th Jul 2016, 11:01 PM
Loic Alou
Loic Alou - avatar
9 Answers
+ 3
After "myFunction" you need parentheses as such: function myFunction (){ / / stuff } You need the parentheses even if you have no parameters. Hope I helped!
13th Jul 2016, 12:58 AM
Cocoa
Cocoa - avatar
+ 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
14th Jul 2016, 3:00 PM
ZinC
ZinC - avatar
+ 1
thx for the article, I appreciate it, I'll look over it :D
15th Jul 2016, 2:06 AM
Cocoa
Cocoa - avatar
0
And you missed the semicolon
13th Jul 2016, 8:23 PM
IvĂĄn Coellito Riverita
IvĂĄn Coellito Riverita - avatar
0
In my experience, semicolons in javascript didn't matter.
13th Jul 2016, 8:25 PM
Cocoa
Cocoa - avatar
0
@Cocoa But they're extremely recommended
13th Jul 2016, 9:16 PM
IvĂĄn Coellito Riverita
IvĂĄn Coellito Riverita - avatar
0
I suppose, I'll credit you that, it'll work either way so whatever haha :D
13th Jul 2016, 9:22 PM
Cocoa
Cocoa - avatar
0
@ Cocoa, will 'var x = 5' work in a browser that doesn't support ECMAscript6?
14th Jul 2016, 6:20 AM
ZinC
ZinC - avatar
0
@ZinC Yes
14th Jul 2016, 12:02 PM
IvĂĄn Coellito Riverita
IvĂĄn Coellito Riverita - avatar