Using Next-Gen JavaScript Syntax
let
and const
const userName = 'John';
let age = 33;
email = 'john.doe@gmai.com'
Arrow Function
const add = (a: number, b: number) => {
return a+b;
};
const substract = (a: number, b: number = 1) => a - b;
const printOut = (output:any) => console.log(output);
printOut("Adding:")
printOut(add(2,2));
printOut("Substracting:")
printOut(substract(4,2));
Default Arguments
const sum = (a: number, b?: number): number => a + (b ?? 0)
const multiply = (a: number, b: number = 1) => a*b;
console.log(add(1,2));
console.log(multiply(3));
Spread Operator
const hobbies = ['Tennis', 'Running', 'Reading']
const activeHobbies = ['Learning']
activeHobbies.push(...hobbies);
const person = {name: 'Max', age: 40}
const anotherPerson = { ...person }
console.log(anotherPerson)
Rest parameters
const addRest = (...numbers: number[]) => {
return numbers.reduce((curResult, curValue) => curResult + curValue)
}
const addNumbers = addRest(1,2,3,4)
console.log(addNumbers)
Array and Object Destructuring
const [hobby1, hobby2, ...reamining] = hobbies;
console.log(hobby1);
console.log(hobby2);
const { name } = person
console.log(name)