Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Peter Webb
Peter Webb
2,635 Points

JavaScript ES2015 workspace issues with ES2015 template literals

I'm still new to coding so I am really uncertain if there is something in the way I've written the code, but after trying it through the workspace itself and through Notepad++, the code works but the workspace behaves pretty weirdly. Determined it's not my own shortcomings and since it might be a bug, although maybe a relatively small one, I contacted the support team. They said it was not a query for them but one for the forums, so awwaaaaay we go.

Quite long winded, sorry about that, it's quite hard to explain the problem without forwarding pictures or being able to talk and walk through it.

The full JavaScript code:

const toggleList = document.getElementById('toggleList');
const listDiv = document.querySelector('.list');

const descriptionInput = document.querySelector('input.description');
const descriptionP = document.querySelector('p.description');
const descriptionButton = document.querySelector('button.description');

const addItemInput = document.querySelector('input.addItemInput');
const addItemButton = document.querySelector('button.addItemButton');

toggleList.addEventListener('click', () => {
  if (listDiv.style.display == 'none') {
      toggleList.textContent = 'Hide list';                        
      listDiv.style.display = 'block';
    } else {
      toggleList.textContent = 'Show list';
      listDiv.style.display = 'none';
  }
});

descriptionButton.addEventListener('click', () => {
  descriptionP.innerHTML = `${descriptionInput.value}:`;
});

addItemButton.addEventListener('click', () => {
  let ul = document.getElementsByTagName('ul')[0];
  let li = document.createElement('li');
  li.textContent = addItemInput.value;
  ul.appendChild(li);
});

I've ran this code through the workspace and it works fine. I've downloaded the file and ran it through Notepad++, worked fine. On both occasions I opened the console for any error logs; none. But the weird thing is on the workspace, everything shown after the closing back-tick in the descriptionP.innerHTML code block...

;
});

addItemButton.addEventListener('click', () => {
  let ul = document.getElementsByTagName('ul')[0];
  let li = document.createElement('li');
  li.textContent = addItemInput.value;
  ul.appendChild(li);
});

...appears as though the workspace thinks its part of the template literal string text. It loses its usual red/green colour, and becomes a navy/dark blue. But like I mentioned earlier, the code still works - which it obviously shouldn't if it was a string, especially a string without closing marks.

In addition, you know when you hover your text cursor next to a bracket the program will highlight the opposing bracket? Well, when I hover the text cursor next to the closing template literal curly brace it highlights the opening function curly brace. When I hover the text cursor next to the opening template literal, it highlights the closing function curly brace. Pretty weird! It doesn't do this for the if statement earlier in the code.

Finally, I noticed how when you usually start typing in the workspace it offers some predictive drop down menu, as many coding programs do. However, when using it after the closing back-tick it stops working for some commands, and still works correctly for others. For example, writing document.querySelector does not load the usual drop menu showing "querySelector" or "querySelectorAll" as I'm typing.

The whole issue seems to centre around the ES2015 template literal. I tried deleting the opening back-tick and nothing changed - the console said "Unexpected token {". I tried deleting the closing back-tick and the text changed back to the usual green/red colour, the bracket thing persisted - the console said "Unterminated template literal". I tried moving the code block to above the toggleList.addEventListener block, and it changed that blocks text colour below to navy blue, and the same predictive drop menu issue happened.

Am I doing something wrong? Since the code still works, I suppose it's not a major issue, but has anyone else experienced this?

2 Answers

Steven Parker
Steven Parker
230,248 Points

I've gotten used to it, but it seems that the workspace editor has yet to be updated to recognize some of the newer JavaScript syntax, including template literals.

But as you noticed, this doesn't prevent things from actually executing properly.

Peter Webb
Peter Webb
2,635 Points

I see, thanks for replying!