JavaScript
querySelector
create a variable for fields in the html
input type="submit" value="Submit guess" class="guessSubmit" />
const guessSubmit = document.querySelector(".guessSubmit");
Event Listeners
.addEventListener("click", checkGuess); guessSubmit
Updating Text
const output = document.querySelector('.output');
const para = document.createElement('p');
.textContent = "Paragraph"; para
Objects
= {species: 'Bird'} bird
Lambdas
const filtered = cats.filter((cat) => cat.startsWith("L"));
Local Storage
.setItem("name", myName);
localStorageconst name = localStorage.getItem("name");
.removeItem("name");
localStorage
// clear all
.clear() localStorage
Javascript vs. React
Data Storage
- Javascript stores variables in the DOM - the representation of HTML nodes.
- React stores variables in Javascript objects, which can be updated as they change on the page
so React will be better for pages with constantly changing user inputs
Promises
doSomething()
.then((result) => doSomethingElse(result))
.then((newResult) => doThirdThing(newResult)) // newResult is the result of doSomethingElse
.then((finalResult) => {
console.log(`Got the final result: ${finalResult}`);
}).catch(failureCallback);
as a good general rule of thumb, always return the contents of a promise
preventDefault
preventDefault is an event listener stops default behaviors of events. Ex: page reload on form submit