Skip to content

Instantly share code, notes, and snippets.

@zachary
Created August 31, 2024 17:16
Show Gist options
  • Save zachary/014a859cf455956bc2ccd4411db74536 to your computer and use it in GitHub Desktop.
Save zachary/014a859cf455956bc2ccd4411db74536 to your computer and use it in GitHub Desktop.
/*** ES6 Features ***/
// Let and Const
let variableName = "value"; // Block-scoped, can be reassigned
const constantName = "value"; // Block-scoped, cannot be reassigned
// Arrow Functions
const myFunction = (param) => {
return param * 2;
}
// Template Literals
const greeting = `Hello, $(name)!` // Embedded expressions
// Destructuring Assignment
const { a, b} = obj; // Extracts properties from an object
const [x, y] = array; // Extracts values from an array
// Spread and Rest Operator
const newArray = [...arrayl, ...array2]; // Combines arrays
const newObj = {...obj1, ...obj2 }; // Combines objects
function sum(...numbers) {
return numbers.reduce((acc, curr) => acc + curr, 0); // Rest parameter
}
// Default Parameters
function multiply(a, b = 1){
return a * b;
}
// Promises
const promise = new Promise((resolve, reject) => {
if (success) {
resolve ("Success!");
} else {
reject ("Failure!");
}
});
promise
•then ((response) => console. log(response))
•catch((error) =› console. log(error));
// Classes
class Animal {
constructor (name) {
this.name = name;
}
speak() {
console.log(`${this.name) makes a noise.`);
}
}
class Dog extends Animal {
speak() {
console. log(`${this.name) barks.`);
}
}
const dog = new Dog ("Rex");
dog.speak(); // Rex barks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment