JavaScript
FAQ
Published on: Jun 7, 2024
How to use template literals?
const firstName = 'John';const lastName = 'Doe';const fullName = `${firstName} ${lastName}`; // John Doe
// The same code using string concatenationconst fullName = firstName + ' ' + lastName; // John Doe
When to use?
- When you need to concatenate strings
How to inject JavaScript into HTML?
<html> <body> <script type="module"> console.log("Hello World!") </script> </body></html>
- paste the JavaScript code inside the
<script>
tag. - use
type="module"
to indicate that the script should be treated as a JavaScript module .
DOM API
How to create an element?
document.createElement('div');
How to specify the element’s text?
element.textContent = 'Hello, world!';
How to append the element to the body?
document.body.append(element);
How to get element by id?
document.getElementById('myElement');
How to specify the element’s class?
element.className = 'my-class';