Skip to content

1. Arrow Functions

js
// Expression bodies
var odds = evens.map(v => v + 1);
var nums = evens.map((v, i) => v + i);
var pairs = evens.map(v => ({even: v, odd: v + 1}));

// Statement bodies
nums.forEach(v => {
  if (v % 5 === 0)
    fives.push(v);
});

// Lexical this
var bob = {
  _name: "Bob",
  _friends: [],
  printFriends() {
    this._friends.forEach(f =>
      console.log(this._name + " knows " + f));
  }
}

2. Classes

js
// Example of using Classes
class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    sayHello() {
      console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
    }
}
const person = new Person("John Doe", 30);
person.sayHello(); // Output: "Hello, my name is John Doe and I am 30 years old."