Arrow Function vs Normal Function

codesplash
Apr 20, 2024

--

#6 JavaScript Series

Syntax

Normal Function

function square (num) {
return num*num;
}

Arrow Function

const square = (num) => {
return num*num;
}

// If there is only one argument, parentheses are optional
// If there is only one operation performed, 'return' and '{}' are optional

const square = num => num * num;

‘arguments ’ Keyword

Normal Function

function hello () {
console.log(arguments);
}
hello ("Deep"); // Output = Deep

Arrow Function

const hello = () => console.log(arguments);

hello ("Deep"); // Output = Error: arguments is not defined

‘This’ Keyword

let user = {
name: "Deep"
f1:()=> console.log(this.name) // with Arrow function,
//this points to the global object

f2(){ console.log(this.name) } // with Normal function,
//this points to the caller function
}
user.f1(); // Output: empty field
user.f2(); //Output:Deep

--

--

codesplash

Currently Moulding a New Life Through Coding || Follow me on linkedin codessmasherdeepjyotidas