javascript

References

Summary

Dynamically typed Syntax is based on C

Scope

var: declares a variable locally (or globally if not in any function)

let: declares block-scope variable

const: declares block-scope constant

Data Types

Boolean

null

Lower case

undefined

Number

BigInt

Arbitrary precision

String

Symbol

Immutable

Object

Promise

Represents the eventual completion (or failure) of an asynchronous operation, and its resulting value

Literals

The values that variables take

Arrays

var people = ["Tim", , "John", ];

Putting multiple commas allows you to create empty items

Also 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

Booleans

Numbers

Objects

Basically dictionaries

const colours = {
  key: value
};

Regex

Strings

Control Flow

Block statements are things contained in { }

if…else

if (condition) {
  statement;
} else if (condition) {
  statement;
} else {
  statement;
}

switch case

switch (expression) {
  case label:
    statement;
    break;
  case label:
    ...
  default:
    defaultStatement;
}

default is only done if no case matches

Operators

Await

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

Errors

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);  

Time

Set Interval

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();

Resource Saving

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

Arrow Functions

React

Declarative rather than imperative

Encourages you to design a bunch of small components

Creates a virtual DOM

Reflow

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

Batching

React sends updates when the current task is finished

Browsers try to refresh every 16ms (60fps)

Components

They return markup

export default

Specifies the main component of the file

Can be functional or object based

Event Handlers

You can place functions into your components, for things like onClick{function-when-clicked}

Hooks

Used in functional components

They start with the use keyword

  1. 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:

    1. the current state (count)
    2. a ways to update the state (setCount)
  2. 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)

    1. Prop Drilling

    2. Key Prop

Classes

CSS classes are denoted by className, rather than class.

JSX (JavaScript XML)

They have tags The browser can't read JSX, so it has to be passed to a transpiler, which converts it into HTML + JS

Router

Life Cycle

  1. Mounting
  2. Updating
  3. Unmounting