Dynamically typed Syntax is based on C
var: declares a variable locally (or globally if not in any function)
let: declares block-scope variable
const: declares block-scope constant
Lower case
Arbitrary precision
Immutable
Represents the eventual completion (or failure) of an asynchronous operation, and its resulting value
The values that variables take
var people = ["Tim", , "John", ];
Putting multiple commas allows you to create empty items
/* empty /* in the empty items, to
inform the readerAlso final commas are ignored
var people = [
"John",
"Tim",
"Greg",
];
so if you add more elements to the array, you don't have to place a comma on a line that previously didn't have one
Basically dictionaries
const colours = {
key: value
};
Block statements are things contained in { }
if (condition) {
statement;
} else if (condition) {
statement;
} else {
statement;
}
switch (expression) {
case label:
statement;
break;
case label:
...
default:
defaultStatement;
}
default is only done if no case matches
Can only be used inside of an async function or at the top level of a module
Causes the program to pause until the relevant promise is completed
Use try and catch
throw raises an exception finally always executes
try {
...
} catch (error) {
} catch (error) {
} finally {
}
throw ErrorName("Error Message");
...
console.error(Error.name);
console.error(Error.message);
Instead of doing a count down, JS will tell the system to notify it in X Minutes The process then gets suspended
function startClockSync() {
const now = new Date();
const minutes = now.getMinutes();
// Calculate minutes until the next :30
let delayMinutes;
if (minutes < 30) {
delayMinutes = 30 - minutes;
} else {
delayMinutes = (60 - minutes) + 30;
}
// Convert to milliseconds
const delayMs = delayMinutes * 60 * 1000;
// Wait for the specific "sync" time
setTimeout(() => {
triggerPopup(); // Show the first one
// After the first sync, repeat every 60 minutes
setInterval(triggerPopup, 3600000);
}, delayMs);
}
function triggerPopup() {
const popup = document.getElementById('reminder');
popup.classList.add('show');
alertSound.play().catch(() => {});
setTimeout(() => popup.classList.remove('show'), 10000);
}
startClockSync();
SVGs and Icon Fonts are much smaller and faster than pngs or jpgs
Don't use gaussian blurs (backdrop-filter: blur() )
Gaussian blurs basically look at the surrounding pixels to figure out how to colour one single pixel Semi-transparency just looks at a single pixel on the layer above/below the target pixel
Declarative rather than imperative
Encourages you to design a bunch of small components
Creates a virtual DOM
Whenever you change the real DOM, the browser has to recalculate the entire page. This is Reflow
The virtual DOM makes all 10 changes first (assuming their caught in the same batch) in a JS object (which is fast) and then sends a single update
React sends updates when the current task is finished
Browsers try to refresh every 16ms (60fps)
They return markup
export default
Specifies the main component of the file
Can be functional or object based
You can place functions into your components, for things like onClick{function-when-clicked}
Used in functional components
They start with the use keyword
Use State
You can declare state variables inside of components
function MyButton() {
const [count, setCount] = useState(0);
// sets the initial count to 0
function handleClick() {
setCount(count + 1);
}
}
useState gives you two things:
Props
Normally when you have a state, it affects only the component it's attached to
To make them use the same state, you first move the hook into the
closest component containing them. Then you call count and setCount
as variables (called props)
Prop Drilling
Key Prop
CSS classes are denoted by className, rather than class.
They have tags The browser can't read JSX, so it has to be passed to a transpiler, which converts it into HTML + JS