# JavaScript 30
# CSS Variables
- Declared in the
:rootwith--varname - To use them ->
var(--name) nodeList !== array- Attributes that start with
data-xxxare custom attributes, made up. We need to prefix them withdata-(spec) this.datasetwill select thedata-xxxattributes- If you update a
css variable, all the selectors that use it will update as well - Example:
- Declaring it (usually in the
:root:element { --main-bg-color: brown; } - Using it:
element { background-color: var(--main-bg-color); }
- Declaring it (usually in the
# Array Cardio
Array.sort-> callback function establishing the sorting order. For example:(a,b) => (a-b)Array.reduce-> is like one of thoseforloops where you add to a count variable.reducefirst takes a callback function withaccumulator, currentvalue, then another argument with the initial value.console.tablereturns a tableArray.from()creates an array from another type of data, like DOM nodes- You can call
querySelectoron other things, not only thedocumentobject Array.includes()returnsbooleanon the argument.sometests if at least one element passes the test of the callback fn..findreturns the value of the first element that passes the test callback..findIndexreturns the index of the first element that passes the test
# Ajax Type-Ahead
- When fetching, we always have to use the
.json()prototype included in promises - We cannot include a variable in a regex: we need to create a new regex variable by doing
new RegExp(variable, 'gi')for example - In an
inputit is common to addchangeandkeyUplisteners
# Chrome Console Tricks
- Right click -> break on -> subtree modifications
console.assert()only fires if it is wrong. Tests something in the first element.console.dirconsole.time,console.timeEndconsole.table
# Shift Checkboxes
- Flag variable -> we declare a Boolean variable, for example
let food = false;. Then we can check for a condition (if statement) and inside that block setfood =!food. This will toggle betweentrueorfalsewithout having to manually set it. - To check two expressions:
food === 'good' || food === 'bad', and NOTfood === 'good' || 'bad'.
# Custom Video Player
# Konami Code
const pressed = [];
const secretCode = 'wesbos';
window.addEventListener('keyup', (e) => {
console.log(e.key);
pressed.push(e.key);
pressed.splice(-secretCode.length - 1, pressed.length - secretCode.length);
if (pressed.join('').includes(secretCode)) {
//We can do anything we want in
}
});
# Slide in on scroll animation
Debouncer function: to save a function to execute constantly. We do this to enhance perfomance. Currently used in
scrollEvents.We calculate our scroll position and add a CSS class to the image when we scroll to a desired position. In this case, when we reach half of the image, we add the class that shows the image with the slidein effect (CSS)
function checkSlide() {
sliderImages.forEach(sliderImage => {
// half way through the image
const slideInAt = (window.scrollY + window.innerHeight) - sliderImage.height / 2;
// bottom of the image
const imageBottom = sliderImage.offsetTop + sliderImage.height;
const isHalfShown = slideInAt > sliderImage.offsetTop;
const isNotScrolledPast = window.scrollY < imageBottom;
if (isHalfShown && isNotScrolledPast) {
sliderImage.classList.add('active');
} else {
sliderImage.classList.remove('active');
}
});
}
# Event delegation and localStorage
It is better to listen for
submitevents rather because we cover cases where the user pressesEnteror clicks with just that listener.Forms have a built-in method called
reset().We can check the
localStoragein DevTools -> Application -> StorageWe need to
JSON.stringify()our items to see them inlocalStorage. We can also parse them afterwards.Event delegation: we listen on the parent, and if the child matches what we want, we select it. Better than listening to all the children.
# CSS text shadow mouse move effect
We can add multiple
text-shadowCSS to the same element.If we are working with mouse position events like
offsetXandoffsetY, the positions reset once we enter the child elements of where we added ourmousemovelistener.
# Sorting band names without articles
- We can do whatever we want inside of the
sort()function. This will affect the sorting itself but NOT the final result of the sorted value.