🌟 Functions & If-Else in JavaScript: Beginner’s Guide

Functions are the building blocks of JavaScript. They let us reuse code, make our programs cleaner, and reduce repetition. In this guide, we’ll explore functions, return values, and decision-making with if-else using simple examples.


🔹 What is a Function?

A function is a block of code designed to perform a particular task.

👉 You define a function once and can reuse it whenever needed.

✅ Function Syntax

function functionName(parameters) {
    // code to be executed
}
  • functionName → Name of the function (e.g., greet, sum)

  • parameters → Input values (like variables)

  • return → Used to send output back from the function


1️⃣ Function Declaration & Invocation

Example: Printing "Hello World"

function printHelloworld() {
    console.log("Hello World");
}
printHelloworld(); // Function call

👉 A function must be invoked (called) to run its code.


2️⃣ Passing Arguments to Functions

Functions can take inputs (parameters).

Example: Greeting Someone

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

greet("Virat Kohli");
greet("Kamlesh");

📝 Output:

Namaste, Virat Kohli
Namaste, Kamlesh

💡 Functions become more dynamic when we pass values.


3️⃣ Functions with Calculations

Let’s create functions that add and multiply numbers.

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

sum(1, 3);  // Output: 4
sum(2, 3);  // Output: 5
function multiply(a, b) {
    let multiple = a * b;
    console.log(multiple);
}

multiply(3, 2);  // Output: 6

4️⃣ Returning Values from Functions

Instead of just printing, functions can return values for later use.

function square(x) {
    let result = x * x;
    return result;
}

let value = square(3);
console.log(value); // Output: 9

💡 Difference:

  • console.log() → Just displays result

  • returnSends result back to be stored or reused


🧩 Decision Making with If-Else

Programs often need to make choices. In JavaScript, we use if-else statements for decision-making.


Example 1: Voting Eligibility

function eligibleToVote(age) {
    if (age < 1) {
        console.log("Invalid Input");
    } else if (age < 18) {
        console.log("Not Eligible");
    } else {
        console.log("Eligible to Vote");
    }
}

eligibleToVote(0);   // Invalid Input
eligibleToVote(15);  // Not Eligible
eligibleToVote(18);  // Eligible to Vote

Example 2: Even or Odd Check

function isEvenOdd(n) {
    if (n % 2 == 0) {
        console.log("Number is Even");
    } else {
        console.log("Number is Odd");
    }
}

isEvenOdd(16); // Output: Number is Even
isEvenOdd(7);  // Output: Number is Odd

💡 % is the modulus operator → gives remainder after division.


⚡ Quick Recap

ConceptExample
Function Declarationfunction greet(name) { ... }
Function Callgreet("Kamlesh")
Parametersfunction sum(a, b) { ... }
Return Valuereturn x * x;
If-Elseif (age < 18) { console.log("Not Eligible") }
Even-Odd Checkif (n % 2 == 0) → Even, else Odd

🎯 Key Takeaways

  • Functions help in code reusability.

  • return makes functions more powerful than just logging.

  • if-else allows decision making in programs.

  • Always test your function with different inputs.


✨ With this knowledge, you can now start writing reusable, dynamic, and interactive programs in JavaScript!


0
Subscribe to my newsletter

Read articles from Kamlesh Choudhary directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kamlesh Choudhary
Kamlesh Choudhary