How do I fix this missing semicolon syntax error in Javascript?

限于喜欢 提交于 2019-11-27 00:48:01

问题


A friend wrote some code for me, and there was one file with a weird syntax error in it. After a bit of hunting, I narrowed it down to this section of code, which should reproduce the error:

var say = functіon(message) {
  alert(message);
  return message;
};

say(say("Goodbye!"));

When I run this, I see an error in the Internet Explorer console that says SCRIPT1004: Expected ';'. I don't see a semicolon missing anywhere, and I can't imagine where it wants me to put one.

Where does it expect a semicolon and why does it expect a semicolon there?


回答1:


Your issue is the fact that the i in function is the unicode character i. If you change it to a 'normal' i it should just work.

But now I'm wondering how the hack :) did you get an unicode character there :P




回答2:


You have misspelled the "function" :)

var say = function(message){
    alert(message);
    return message;
};

say(say("Goodbye!"));

You have inserted functіon :)




回答3:


I've copied and pasted it in my notepad++ and your code look like this in my notepad++, retype your function keyword, i is replaced by ?.

var say = funct?on(message) {
      alert(message);
      return message;
    };
    say(say("Goodbye!"));



回答4:


I copied your code into jsfiddle, and Chrome too gives an error. I deleted the word "function", and re-typed "function", and it worked fine.

There must be some extra character there.




回答5:


I had a similar problem and the same error code when debugging someone else's work. To fix this I pasted the section of code into Notepad and then re-copied it back to Visual Studio. The error went away. I think whoever wrote the code originally must have copied it from somewhere with some strange characters in it.




回答6:


In fact, you inserted unicode "i" instead of normal "i". I get the fellow errors in VSCode:
',' expected. (1, 29)
',' expected. (2, 10)
Declaration or statement expected. (4, 3)
You can try evaluating "functіon" == "function" as well:

function compare() {
  return "functіon" === "function"
}
console.log(compare())

However, when I try to compare it by drawing "function" myself: it returns true;

function compare2() {
  return "function" == "function"
}
console.log(compare2())

Also, I didn't include semicolons here, in javascript they aren't necessary.

来源:https://stackoverflow.com/questions/9246051/how-do-i-fix-this-missing-semicolon-syntax-error-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!