JavaScript Best Practices
Strings
Use template literals
For inserting values into strings, use string literals.
Do this:
let myName = 'Chris'; console.log(`Hi! I'm ${myName}!`);
Not this:
let myName = 'Chris'; console.log('Hi! I\'m' + myName + '!');
Use textContent, not innerHTML
When inserting strings into DOM nodes, use Node.textContent
:
let text = 'Hello to all you good people'; const para = document.createElement('p'); para.textContent = text;
Not Element.innerHTML
let text = 'Hello to all you good people'; const para = document.createElement('p'); para.innerHTML = text;
textContent
is a lot more efficient, and less error-prone than innerHTML
.