DEV Community

codeToBug
codeToBug

Posted on • Originally published at codetobug.dev

JavaScript Face-off: Traditional Functions vs. Arrow Functions – An Absurdly Dramatic Analysis

Ready to witness a cosmic battle between JavaScript's Old Guard (traditional functions) and New Blood (arrow functions)? Strap in, folks. It's about to get oddly... informative.

1. Syntax – Dress Code of Functions

Who likes dressing up, right? Traditional functions insist on it – the full regalia, with the function keyword, parentheses, and curly braces. Arrow functions, however, are your casual-Friday heroes. Just an arrow (=>) and we’re rolling!

Traditional Function:

function getDressed() {
    console.log("I need a tie, a suit, and a shiny pair of shoes.");
}

getDressed(); // "I need a tie, a suit, and a shiny pair of shoes."
Enter fullscreen mode Exit fullscreen mode

Arrow Function:

let getUndressed = () => console.log("Pajamas, here I come!");

getUndressed(); // "Pajamas, here I come!"
Enter fullscreen mode Exit fullscreen mode

2. Use of the 'this' Keyword – Who am I, again?

Traditional functions have identity crises. They can't decide what 'this' is until runtime. "Am I a window object? A button? Who knows? Guess we'll find out!" Arrow functions, on the other hand, just inherit 'this' from their surroundings like they’ve got it all figured out!

Traditional Function:

let traditional = {
    name: "Old School",
    whoAmI: function() { console.log(this.name); }
}

traditional.whoAmI(); // "Old School"
Enter fullscreen mode Exit fullscreen mode

Arrow Function:

let arrow = {
    name: "New Kid",
    whoAmI: () => { console.log(this.name); }
}

arrow.whoAmI(); // "undefined" (Because Arrow function doesn't know who it is)
Enter fullscreen mode Exit fullscreen mode

3. Access to Arguments – Guess who’s a bit nosy?

Traditional functions like gossip. They have access to all arguments with the "arguments" object, because who doesn't love some extra chatter? Arrow functions, however, are all about the "you do you" vibe. No arguments object for these hipsters!

Traditional Function:

function whoAmI() {
    console.log(arguments[0] + " " + arguments[1]);
}

whoAmI("Hello", "World"); // "Hello World"
Enter fullscreen mode Exit fullscreen mode

Arrow Function:

let whoAmI = () => {
    console.log(arguments[0] + " " + arguments[1]);
}

whoAmI("Hello", "World"); // Throws an error: arguments is not defined
Enter fullscreen mode Exit fullscreen mode

4. Using the New Operator – Trying to be the Constructor Boss

Like a boss, traditional functions can be constructors. Just slap on the new operator and BAM – you've got yourself an object! Arrow functions, however? Too cool for school, and certainly too cool for the 'new' operator. Constructors? We don’t need no stinking constructors!

Traditional Function:

function Boss(name) {
    this.name = name;
}

let myBoss = new Boss("Big Boss");
console.log(myBoss.name); // "Big Boss"
Enter fullscreen mode Exit fullscreen mode

Arrow Function:

let NotBoss = (name) => { this.name = name; }
let myNotBoss = new NotBoss("Mini Boss"); // Throws an error: NotBoss is not a constructor
Enter fullscreen mode Exit fullscreen mode

5. Parameters with Duplicate Naming – Because Naming is Hard, Okay?

Traditional functions are so chill, they won't mind if you name two parameters the same. Go ahead, confuse yourself, they don't care. Arrow functions, though? They have the audacity to throw an error. How dare they enforce good coding practices!

Traditional Function:

function add(num1, num1) {
    return num1 + num1;
}

console.log(add(2, 3)); // 6 (num1 is 3)
Enter fullscreen mode Exit fullscreen mode

Arrow Function:

let add = (num1, num1) => {
    return num1 + num1;
} // Throws an error: Duplicate parameter name not allowed in this context
Enter fullscreen mode Exit fullscreen mode

6. Implicit One Line Return – The Silent Hero

Arrow functions can do a neat party trick - returning values without the 'return' keyword, because, you know, less is more. Traditional functions will just stare at you blankly, refusing to do the job unless you tell them explicitly.

Traditional Function:

function getNumber() {
    return 10;
}

console.log(getNumber()); // 10
Enter fullscreen mode Exit fullscreen mode

Arrow Function:

let getNumber = () => 10;

console.log(getNumber()); // 10 (Without the 'return' keyword. So cool!)
Enter fullscreen mode Exit fullscreen mode

So there you have it, folks. JavaScript's heavyweight title fight. It's a dazzling display of technical quirks, with no clear winner in sight! What's next? A function that makes coffee? Only time will tell!

Top comments (5)

Collapse
 
mohamadg profile image
mohamadg

Liked your style...

Collapse
 
codetobug profile image
codeToBug

ooh! thank you! but it is a joint work with chatGPT. My crazy mind with the cunning of chatGPT

Collapse
 
mohamadg profile image
mohamadg

I see... It's quiet unfortunate that openai.com/ team have blocked connections from some countries including mine... :(

Thread Thread
 
noriste profile image
Stefano Magni

It was the same here in Italy then they fixed the compatibility issues, I hope the same will happen to your country 😊

Thread Thread
 
mohamadg profile image
mohamadg

Thanks man, appreciate it.🙏