46

I tested this javascript in Chrome's Javascript console and it returned SyntaxError: Unexpected Identifier.

I got this code from a tutorial and was just testing Chrome's console so i expected it to work, unless I'm using the console wrong?

Code:

var visitorName = "Chuck";
var myOldString = "Hello username. I hope you enjoy your stay username.";
var myNewString = myOldString.replace ("username," visitorName);

document.write("Old String = " + myOldString);
document.write("<br/>New string = " + myNewString);

Output:

SyntaxError: Unexpected identifier
SamB
  • 9,039
  • 5
  • 49
  • 56
mjmitche
  • 2,077
  • 5
  • 24
  • 31
  • Same error for a different reason https://stackoverflow.com/questions/72127339/getting-this-error-on-running-js-using-node-uncaught-syntaxerror-unexpected-i – Michael Freidgeim Mar 20 '23 at 12:19

6 Answers6

98

The comma got eaten by the quotes!

This part:

("username," visitorName);

Should be this:

("username", visitorName);

Aside: For pasting code into the console, you can paste them in one line at a time to help you pinpoint where things went wrong ;-)

David Tang
  • 92,262
  • 30
  • 167
  • 149
7

Replace

 var myNewString = myOldString.replace ("username," visitorName);

with

 var myNewString = myOldString.replace("username", visitorName);
Robin
  • 21,667
  • 10
  • 62
  • 85
1

I got this error Unexpected identifier because of a missing semi-colon ; at the end of a line. Anyone wandering here for other than above-mentioned solutions, This might also be the cause of this error.

M. Habib
  • 623
  • 1
  • 9
  • 15
0

Write it as below

<script language="javascript">
var visitorName = 'Chuck';
var myOldString = 'Hello username. I hope you enjoy your stay username.';

var myNewString = myOldString.replace('username', visitorName);

document.write('Old String = ' + myOldString);
document.write('<br/>New string = ' + myNewString);
</script>

http://jsfiddle.net/h6xc4/23/

ashish.chotalia
  • 3,696
  • 27
  • 28
  • 9
    You really should point out the problem. The OP didn't notice the comma on his/her own code, and isn't likely to spot the change. From a quick look, it looks like you're suggesting to use single quoted string instead of double quoted, which can be confusing - they are the same in JavaScript, but not in all languages (PHP, for one). – Kobi Feb 15 '11 at 05:26
  • 1
    I tried to place myOldString.replace('username', visitorName); in bold but some how if you are placing your code in code section it didn't output it bold. – ashish.chotalia Feb 15 '11 at 05:27
  • You can still edit your answer, of course. (Code's already bolded on SO.) –  Feb 15 '11 at 05:47
0

copy this line and replace in your project

var myNewString = myOldString.replace ("username", visitorName);

there is a simple problem with coma (,)

MD SHAYON
  • 7,001
  • 45
  • 38
0

I got SyntaxError: Unexpected identifier error, for invalid order between async and static, static must come first.

//Invalid
async static methodName() {

}

//Valid
static async methodName() {

}
ahmad pirzargar
  • 129
  • 3
  • 7