Javascript - SyntaxError: Invalid or unexpected token - while creating object - Invisible character

12,838

If you copy everything you just pasted and write it in the console, you would see that there are some unicode characters (\u200b) in your code which are actually the Invalid or unexpected token in the error that you are getting, you don't see them because they are zero-width spaced, so just remove them and the code would run perfectly as shown below

// We have been using dot notation so far in the examples above, here is another example again:
var book = {title: "Ways to Go", pages: 280, bookMark1:"Page 20"};

// To access the properties of the book object with dot notation, you do this:
console.log(book.title); // Ways to Go
console.log(book.pages); // 280

You can find more about the zero width space character here: http://www.fileformat.info/info/unicode/char/200b/index.htm

Share:
12,838
ross-u
Author by

ross-u

Full-Stack Engineer working with key JavaScript technologies: Node.js, Koa, Express, MongoDB, SQL, React/Redux and React Native. Fast-learner, passionate about computer science. Staying current with the new technologies, advancing my technical skills and attending tech meet-ups are my spare time omens.

Updated on June 05, 2022

Comments

  • ross-u
    ross-u almost 2 years

    I was reading the article on Javascript Objects (http://javascriptissexy.com/javascript-objects-in-detail/), so I copy-pasted the following code into my Notepad++ and ran it in Chrome(Version 55.0.2883.87 m). Upon opening the console, console reports SyntaxError. Does someone have an idea why? Everything seems ok.

    // We have been using dot notation so far in the examples above, here is another example again:​
    ​var book = {title: "Ways to Go", pages: 280, bookMark1:"Page 20"};
    ​
    ​// To access the properties of the book object with dot notation, you do this:​
    console.log(book.title); // Ways to Go​
    console.log(book.pages); // 280