ES2019 is the latest version of Javascript. ES6 is commonly used amongst developers and also gets called JavaScript 6… sometimes.
With let
you can change the value of the variable however with const
(short for constant) you are unable to change the value.
for(let i=0; i<=10; i++) {
return i++;
}
render() {
const { example } = "hello"
return (
<div>{example}</div>
)
}
Previously…
function example(a, b) {
return a+b;
}
With ES6
const example = (a,b) => { return a+b; }
class Cat {
constructor(type) {
this.colour = 'ginger';
}
}
class Animal {}
class Cat extends Animal {
sound = 'Meow!';
/*
this is a private class field
#sound = 'Meow!';
console.log(this.#sound);
*/
makeSound() {
console.log(this.sound)
}
}
const minnie = new Cat();
minnie.makeSound();
An example of private properties
class Cat {
#name;
constructor(name) {
this.#name = name;
}
showName() {
console.log(this.#name);
}
}
const minnie = new Cat('Minnie');
minnie.showName();
function example(str) {
return str;
}
example("Hello");
const num = [1,2,3,4]
const check = num.find(theValue);
function theValue(value, index, arr) {
return value < 2;
}
const num = [1,2,3,4]
const check = num.findIndex(theValue);
function theValue(value, index, arr) {
return value < 2;
}
let a = 6;
let b = a ** 2; //b=36
let a = 6;
let b = Math.pow(a, 2); //b=36
Regular Expressions (or RegEx) is a feature which enables you to search patterns within a string.
This is used to select a pattern when another known patter is ahead of.
const str = "crosswords, afterwords, catchwords";
const word = str.match(/[a-zA-Z]+(?=words)/g);
console.log(word); //["cross", "after", "catch"]
A negative lookahead does the opposite to a positive lookahead. Instead it looks for the words that do not include the common string.
const str = "crosswords, afterwards, catchwords";
const words = testString.match(/(cross|after|catch)(?!words)/g);
console.log(words); //["after"]
Used to select a pattern in a string when another known pattern is just behind it.
const str = "frozenberries, frozenyoghurt, frozenfish";
const frozen = str.match(/(?<=frozen)[a-zA-Z]+/g);
console.log(frozen); //["berries", "yoghurt", "fish"]
Select a pattern in a string when another known pattern is not behind it.
const str = "redberries, frozenyogurt, frozenfish";
const frozen = str.match(/(?<!frozen)(berries|yogurt|fish)/g);
console.log(frozen); //["berries"]
const nums = [1, [2, [3, [4, 5]]]];
nums.flat(); //[1,2, [3, [4, 5]]]
nums.flat(1); //[1, 2, 3, [4,5]]
nums.flat(Infinity); //[1, 2, 3, 4, 5]
const nums = [1, 2, 3, 4, 5];
const sqr = nums.map(n=>[n, n*n])
//sqr = [[1,1], [2,4], [3,9]]
sqr.flat(); //[1, 1, 2, 4, 3, 9]
const test = {a:1, b:2, c:3};
const testEntries = Object.entries(test); //[["a", 1], ["b", 2], ["c", 3]]
const test = [["a", 1], ["b", 2], ["c", 3]];
const testFrom = Object.fromEntries(test) //{a:1, b:2, c:3}
this
depends on the context of where it is accessed
Takes an array of promises and resolves once all of the promises have been resolved/rejected
const prom1 = () => new Promise(
(resolve, reject) => setTimeout(() => resolve('one'), 2000);
)
const prom2 = () => new Promise(
(resolve, reject) => setTimeout(() => reject('err'), 2000);
)
var prom = Promise.allSettled([prom1(), prom2()]. then(
(values) => console.log(values)
))
/* output
[
{status: "fulfilled", value: "one"}
{status: "rejected", value: "err"}
]
*/