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?
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);
SyntaxError: Unexpected identifier
To solve the "Uncaught SyntaxError: Unexpected identifier" error, make sure you don't have any misspelled keywords, e.g. Let or Function instead of let and function , and correct any typos related to a missing or an extra comma, colon, parenthesis, quote or bracket.
Uncaught SyntaxError: Unexpected identifier. One of the most common reasons is that you're trying to mutate (change) a const variable. You can't do that. In JavaScript, the keyword const is a so-called immutable variable which is used for variables that you don't want anyone to modify or redeclare.
The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.
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 ;-)
Replace
var myNewString = myOldString.replace ("username," visitorName);
with
var myNewString = myOldString.replace("username", visitorName);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With