Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 308
mjmitche Avatar asked Feb 15 '11 05:02

mjmitche


People also ask

How do I fix 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.

What is unexpected identifier JavaScript?

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.

What is unexpected token error in JavaScript?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.


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

like image 166
David Tang Avatar answered Sep 22 '22 15:09

David Tang


Replace

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

with

 var myNewString = myOldString.replace("username", visitorName); 
like image 42
Robin Avatar answered Sep 22 '22 15:09

Robin