`
Arrow functions are a compact and readable form of normal(traditional) function. In addition, there are some other differences too in the way they work as compared to the normal functions.
let foo = (arg1, arg2) => {
console.log("Inside arrow finction")
}
// Arrow function
var foo = (arg1) => {
console.log("This is a function with arguments: ", param1);
}
// Normal function
function foo(arg1) {
console.log("This is a function with arguments: ", param1);
}
() => {
console.log("This is a function ");
};
function studentArrowFunction(fName, obtainedMarks) {
console.log(this);
this.fNameObj = fName;
this.obtMarksObject = obtainedMarks;
// Normal Function
this.checkResultsNormal = function () {
console.log(this);
function innerFunction() {
console.log("Normal Inner function: ", this);
}
innerFunction();
};
// Arrow Function
this.checkResultsArrow = () => {
console.log(this);
innerFunction = () => {
console.log("Arrow Inner function: ", this);
};
innerFunction();
};
}
var student = new studentArrowFunction("fname5", 100);
student.checkResultsNormal() // Window {window: Window, self: Window, document: document, name: '', location: Location, …}
student.checkResultsArrow() // studentArrowFunction {fNameObj: 'fname5', obtMarksObject: 100, checkResultsNormal: ƒ, checkResultsArrow: ƒ}
// Arrow function
var arrowFunction = () => {
console.log("The default arguments object is: ", arguments)
}
foo("arg1"); // Uncaught ReferenceError: arguments is not defined
// Normal function
function normalFunction() {
console.log("The default arguments object is: ", arguments) // undefined error
}
foo("arg1"); // Arguments ['arg1', callee: ƒ, Symbol(Symbol.iterator): ƒ]