Morning:
Afternoon:
var, const, let)someElement.innerHTMLdocument.createElementsomeElement.style.stylePropertyNamesomeElement.appendChild
// function declaration - use 'function' keyword
function aMostExcellentFunction() {
console.log('This function is great!')
}
aMostExcellentFunction() // => 'This function is great!'
// function expression - defines a function as part of a larger expression syntax
// (usually assignment to a variable)
const anotherExcellentFunction = () => {
console.log('This function is also great!')
}
anotherExcellentFunction() // => 'This function is also great!'
// functions as object properties (also known as 'methods')
const myObject = {
myMethod() {
console.log('I am a method!')
}
}
myObject.myMethod() // => 'I am a method!'
The biggest difference between var and let is that var variables are scoped to the function in which they are declared, while let variables are scoped to the block in which they are declared. One of the easiest examples to see this behavior is in a simple for loop.
function loopStuff() {
for (var i = 0; i < 5; i++) {
// do stuff in the loop
}
console.log(i)
}
loopStuff() // => 5
function loopMoreStuff() {
for (let i = 0; i < 5; i++) {
// do stuff in the loop
}
console.log(i)
}
loopMoreStuff() // => Uncaught ReferenceError: i is not defined
In the function loopStuff, var i is still available outside the for loop so it can be logged to the console. It is scoped to the function itself.
In the function loopMoreStuff, let i is not available outside the block it is scoped to (the for loop).
The main difference between const and var/let is that const cannot be reassigned.
let variableOne = 4
variableOne = 5
var variableTwo = 4
variableTwo = 5
const variableThree = 4
variableThree = 5 // => Uncaught TypeError: Assignment to constant variable
Always use const as your default way to declare variables, unless you know specifically that you will need to reassign it, in which case use let. You should rarely, if ever, use var. For further reading, check out this article
If we start with the following markup:
<div id="my-div"></div>
We can add additional markup to it programmatically using JavaScript. One way is to create new HTMl elements using document.createElement, and adding them by using appendChild. Styling of the element can even be changed by manipulating the element’s style property.
// create an h1 and modify text content and color
const heading = document.createElement('h1')
heading.textContent = "This is the best heading I've ever seen"
heading.style.color = "red"
// get a reference to the existing div and add the heading as a child element
const div = document.querySelector('#my-div')
div.appendChild(heading)
This will produce the following markup:
<div id="my-div">
<h1 style="color: red;">This is the best heading I've ever seen</h1>
</div>
renderColor that returns a div element.div to colorItem.renderListItem.renderList.renderListItem from renderList, not directly from handleSubmit.innerHTML. Keep using document.createElement.