SyntaxError: Unexpected Identifier in Chrome's Javascript console

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
0

6 Answers

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 ;-)

2

Replace

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

with

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

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.

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>
3

copy this line and replace in your project

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

there is a simple problem with coma (,)

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() {
}

You Might Also Like