JavaScript Functions

JavaScript Guide

A Space Journey from Basic Structure to Arrow Functions 🚀🧠

Without functions in JavaScript:

  • Code gets longer
  • Your brain gets messy
  • The question “Why did I write this 15 times?” appears 😵‍💫

With functions:

  • Code gets shorter
  • Logic becomes clear
  • You level up 🎮

In this article:

From “What is a function?” to “Where should I NEVER use arrow functions?”
we explain everything in a human way.


🧩 1. What Is a Function? (The Honorable Way of Being Lazy)

A function means:
👉 Define a task once
👉 Use it as many times as you want

Real-life analogy:

Every morning, you don’t explain the steps of making coffee one by one.
You just say “Make coffee” — and your brain runs it automatically ☕


📌 The Simplest Function Example

function sayHello() {
  console.log("Hello JavaScript!");
}

Now let’s break it down 🧩

  • function → You tell JavaScript “I’m defining a function”
  • sayHello → The function name (it should describe what it does!)
  • {} → The place where the action happens

⚠️ Important:
This code does not run. It’s just a definition.

To make it work:

sayHello();

📌 Golden rule:
Function = recipe
Function() = eat the meal 🍝


🏗️ 2. Parameters: Sending Information to a Function

Functions are like:

“Give me some data, I’ll give you a result.”


🎯 Function with Parameters

function greet(name) {
  console.log("Hello " + name);
}

Let’s call it:

greet("Cansu");
greet("Ahmet");

What happened here?

  • name → parameter (what the function expects)
  • "Cansu" → argument (what you send)

📦 Analogy:

  • Parameter = shipping box
  • Argument = product inside the box

🧠 Multiple Parameters

function introduce(name, age) {
  console.log(`My name is ${name}, I am ${age} years old`);
}

introduce("Ayşe", 25);

📌 Tip:
Parameter order MATTERS.
JavaScript doesn’t guess “Is this Ayşe or 25?” 😄


🔁 3. Return: The Return Ticket from a Function 🎫

One of the most confusing topics for beginners:
console.logreturn


❌ Function That Only Prints

function sum(a, b) {
  console.log(a + b);
}

You can’t use this result elsewhere.


✅ Function with Return

function sum(a, b) {
  return a + b;
}

const result = sum(3, 5);

What did we gain?

  • We stored the result in a variable
  • We can use it elsewhere
  • The function actually did work

📌 Golden tip:
If there’s a return, the function stops there.

function test() {
  return "Finished";
  console.log("This will not run");
}


🧠 4. Function Expression (The Secret Identity of Functions 🕶️)

Functions can be assigned to variables.

const sayHi = function () {
  console.log("Hi!");
};

Run it:

sayHi();

Why use this?

  • More controlled structure
  • Common in modern JavaScript
  • Very popular in callbacks

📌 Info:
These functions are not hoisted.
(Technical but important — you’ll need it later 🔥)


🚀 5. Arrow Functions: The Rockstar of Modern JavaScript 🎸

Introduced with ES6, and everyone fell in love 😄


🔹 Basic Arrow Function

const sayHello = () => {
  console.log("Hello!");
};

Same job, shorter, more modern.


🔥 Single Parameter

const greet = name => {
  console.log("Hello " + name);
};

Parentheses are gone ✌️


⚡ One Line + Automatic Return

const square = x => x * x;

Expanded version:

const square = function (x) {
  return x * x;
};

📌 Golden rules:

  • One parameter → parentheses optional
  • One line → return is automatic
  • Returning an object? Watch out for {} 👇
const createUser = () => ({ name: "Ali" });


⚠️ 6. Arrow Functions & this (The Biggest Trap ☠️)

Arrow functions do not create their own this.


❌ Wrong Usage

const user = {
  name: "Zeynep",
  greet: () => {
    console.log(this.name);
  }
};

Result: undefined 😬
Because this → window / global object


✅ Correct Usage

const user = {
  name: "Zeynep",
  greet() {
    console.log(this.name);
  }
};

📌 Life-saving rule:
👉 Object method = normal function
👉 Callback / short operation = arrow function


🧪 7. Real-Life Arrow Function Usage

🔹 Array Methods

const numbers = [1, 2, 3, 4];

const doubled = numbers.map(n => n * 2);

  • map → iterates over each item
  • n → each number
  • short, clean, readable 💎

🔹 Event Listener

button.addEventListener("click", () => {
  console.log("Clicked!");
});

Arrow functions are perfect here because you don’t need this.


🧠 8. Which One Should I Use? (Mini Guide 📋)

ScenarioUse
Short operationsArrow
Array methodsArrow
Event callbacksArrow
Object methodsNormal function
When this is neededNormal function
Readability mattersArrow

💎 9. Professional Tips (These Set You Apart)

  • Function names should describe what they do
❌ doStuff()
✅ calculateTotalPrice()

  • Functions should be small
  • 1 function = 1 responsibility
  • Arrow functions are not for everywhere
  • Learn to use return → level up 🚀

🎯 Final Thoughts: Make Friends with Functions 🕊️

Functions are:

  • The heart of JavaScript ❤️
  • The key to clean code 🔑
  • What makes you code like a real developer 😎

Arrow Functions are:

  • Modern
  • Short
  • Powerful
    but when used in the wrong place, they produce bugs 😄

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir