Skip to content

Instantly share code, notes, and snippets.

@MeetMartin
Created June 9, 2021 01:09
Show Gist options
  • Select an option

  • Save MeetMartin/364f9e0a7885ce341976af7a65aaee88 to your computer and use it in GitHub Desktop.

Select an option

Save MeetMartin/364f9e0a7885ce341976af7a65aaee88 to your computer and use it in GitHub Desktop.
let state = {
foreground: '#999999',
background: '#FFFFFF';
};
const imperativeMakeBackgroundBlack = () => {
state.background = '#000000';
};
// directly changes the state object outside of its internal scope
const declarativeMakeBackgroundBlack = state => ({...state, background: '#000000'});
// takes current state as its input and returns new state with changed value
// without changing the original state
let turtles = ['Galápagos tortoise', 'Greek Tortoise'];
const imperativeAddTurtle = turtle => turtles.push(turtle);
// changes the turtles external array and returns the length of the new array
const declarativeAddTurtle = turtles => turtle => [...turtles, turtle];
// takes 'array of turtles' and the 'new turtle' as its input.
// It returns new array of turtles without changing the original array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment