achlopecki83
achlopecki83

Reputation: 29

Problem with "Syntax error: unexpected token 'let'

I'm using AptanaStudio 3 and I'm getting an error with some of my javascript code:

Syntax error: unexpected token 'let'.

How do I fix this? Maybe there is some way around using let? I don't know. I've just started with Javascript and I'm a bit confused. Maybe this is only an AptanaStudio 3 issue?

const para = document.querySelector('p');

para.addEventListener('click', updateName);

function updateName() {
    let name = prompt('Enter a new name');
    para.textContent = 'Player1:' + name;
}

This Javascript code adds some dynamic behaviour to my html/css code.

Upvotes: 1

Views: 4176

Answers (2)

I've just tested your code and it works correctly! I think the problem is AptanaStudio, you should read: Does Aptana support ES6?. The thing is, let is an statement that was born with ECMAScript 6, the "new Javascript" ahahah
Read this article to new more about this update on js power: https://www.w3schools.com/js/js_es6.asp
I recommend you look for other IDEs or even start thinking about programming without a real IDE, but with just a text editor like Visual Code or Sublime Text (which I like a lot).
For testing I simply used Visual Studio Code and opened the html file in Google Chrome with the code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>

</head>
<body>
  <p>Player name.</p>
  <script>
    const para = document.querySelector('p');

    para.addEventListener('click', updateName);

    function updateName() {
      let name = prompt('Enter a new name');
      para.textContent = 'Player1:' + name;
    }
  </script>
</body>
</html>

Upvotes: 0

Jack Bashford
Jack Bashford

Reputation: 44145

In this context, var is syntactically identical to let, and is much more widely supported, particularly on Microsoft's Internet Explorer. To make your code fully ECMAScript 5 compatible, it would look like this:

var para = document.querySelector("p");

para.addEventListener("click", updateName);

function updateName() {
    var name = prompt("Enter a new name");
    para.textContent = "Player1:" + name;
}

(Nothing has changed except replacing ES6 const and let with ES5 var.)

Upvotes: 2

Related Questions