Skip to content

Instantly share code, notes, and snippets.

@TaylorBeeston
Last active July 16, 2020 16:20
Show Gist options
  • Save TaylorBeeston/8e1f9108592c0a71dcb64436a7de8eea to your computer and use it in GitHub Desktop.
Save TaylorBeeston/8e1f9108592c0a71dcb64436a7de8eea to your computer and use it in GitHub Desktop.
Javascript Examples
/*
* Below are some examples of the kind of Javascript I like to write
* (Please note, these are contrived examples that might be silly in real life)
*/
// Big Arrow Notation
const addNumbers = (num1, num2) => num1 + num2;
// Object Destructuring and String Interpolation
const anyOrderArgs = ({ title = 'Title', body = 'Body' }) =>
`Title: ${title}\nBody: ${body}`;
// Object shorthand
const filterKeys = ({ title, defaultValue = 'default' }) => ({
title,
defaultValue,
});
// Closures
const counter = (initialCount = 0) => {
let count = initialCount;
const increment = () => {
count += 1;
};
return { count, increment };
};
// Array spreading
const duplicateArray = (array) => [...array];
// Async/Await
const getTodo = async () =>
(await fetch('https://jsonplaceholder.typicode.com/todos/1')).json();
// Functional Loops
const objectifyArrayValues = (array) => array.map((value) => ({ value }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment