JavaScript Notes

Photo by Irvan Smith on Unsplash

JavaScript Notes

Array Functions

In ES5 we use functions as below:

function my_fun(num){
      return num+50;
}
  console.log(my_fun(120));

From ES6 we have Arrow Functions as Below:

const my_fun = (num1) =>{
    return num1+50 ;
}
console.log(my_fun(50));

Without return statement

const my_fun = (num1,num2) => num1 + num2;
console.log(my_fun(50,100));

Classes in JavaScript (ES6)

The following is the Way to Creating Classes in JavaScript

class College{
    constructor(){
        this.clg = "Name Of College";

    }
    getCollege(){
        return this.clg;
    }
}
class Student extends College{
    constructor(){
        super();
        this.name = "Nikhil";
    }
    printName(){
        return this.name ;
    }
}

const Std_obj = new Student();
console.log(Std_obj.printName());
console.log(Std_obj.getCollege());

Output

Nikhil
Name Of College

Overloading

class College{
    constructor(){
        this.clg = "Name Of College";

    }
    getCollege(){
        return this.clg;
    }
}
class Student extends College{
    constructor(){
        super();
        this.name = "Nikhil";
        this.clg = "Vidyanikethan"
    }
    printName(){
        return this.name ;
    }
}

const Std_obj = new Student();
console.log(Std_obj.printName());
console.log(Std_obj.getCollege());

Output

Nikhil
Vidyanikethan

Classes in JavaScript(ES7)

❖ this keyword is not required for declaring variables

❖ Constructor is not required for Initializing values

❖Arrow Functions

class College{
    clg = "Name Of College";
    getCollege = () =>  this.clg;
}
class Student extends College{
    name = "Nikhil";
    clg = "Vidyanikethan"; 
    printName = () =>  this.name ;

}
const Std_obj = new Student();
console.log(Std_obj.printName());
console.log(Std_obj.getCollege());

The above code is of the ES7 version and the output is similar to that of ES6 Class

Nikhil
Vidyanikethan

Spread Operator in JavaScript

In Arrays:

lis1 = [2,3,4]
lis2 = [1,...lis1,5]
console.log(lis2)

Output

[ 1, 2, 3, 4, 5 ]