Using Next-Gen JavaScript Syntax

let and const

/* Using "let" and "const" */

const userName = 'John';
// userName = 'Mark'; (Error)

let age = 33;                   // The `let` declaration declarest a block-scoped local variable
email = 'john.doe@gmai.com'     // The `var` statement declarest a function scoped or globally scoped

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

/* Using Optional / Default Params */
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

/* Using Spread Operators */
const hobbies = ['Tennis', 'Running', 'Reading']
const activeHobbies = ['Learning']

// Spread existing array/objects
activeHobbies.push(...hobbies);

const person = {name: 'Max', age: 40}
const anotherPerson = { ...person }
console.log(anotherPerson)

Rest parameters

/* Using Rest Parameters */
// Rest params in a JavaScript feature allows you to collect the "rest" of the arguments of a function
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

/* Array and Object Destructuring */
const [hobby1, hobby2, ...reamining] = hobbies;
console.log(hobby1);
console.log(hobby2);

const { name } = person
console.log(name)