Track: JavaScript Basics
Lesson: JS-05 / JS-10
π― Lesson Goal
By the end of this lesson, you will understand what functions are, why they are used, and how to create and use different types of functions in JavaScript to write clean, reusable code.
π Prerequisites
- JS-01: Variables and Data Types
- JS-02: Operators and Expressions
- JS-03: Conditional Statements
- JS-04: Loops
You should already be comfortable with:
- Variables
- Conditions
- Loops
π What is a Function?
A function is a block of reusable code that performs a specific task.
Instead of writing the same code again and again, we define it once and reuse it whenever needed.
Real-life example
- Calculator β βAddβ, βSubtractβ
- Mobile β βCallβ, βMessageβ
- Website β βLoginβ, βLogoutβ
πΉ Why Functions Are Important
Functions help to:
- Avoid repetition
- Improve readability
- Make code easier to debug
- Organize large programs
- Build scalable applications (MERN!)
πΉ Defining a Function
Syntax
function functionName() {
// code to execute
}
Example
function greet() {
console.log("Welcome to Programming Empire");
}
greet();
β Output:
Welcome to Programming Empire
πΉ Function with Parameters
Parameters allow functions to accept input values.
Example
function greetUser(name) {
console.log("Hello " + name);
}
greetUser("Aman");
greetUser("Riya");
β Output:
Hello Aman
Hello Riya
πΉ Function with Return Value
A function can return a value using the return keyword.
Example
function add(a, b) {
return a + b;
}
let result = add(10, 20);
console.log(result);
β Output:
30
π΄ Once return is executed, the function stops.
πΉ Function Expression
Functions can be stored in variables.
Example
const multiply = function (x, y) {
return x * y;
};
console.log(multiply(4, 5));
πΉ Arrow Functions (Modern JavaScript)
Arrow functions provide shorter syntax and are widely used in React and MERN.
Syntax
const functionName = (parameters) => {
// code
};
Example
const square = (n) => {
return n * n;
};
console.log(square(5));
One-line Arrow Function
const cube = n => n * n * n;
console.log(cube(3));
πΉ Default Parameters
Default values are used if arguments are not provided.
function greet(name = "Student") {
console.log("Hello " + name);
}
greet();
greet("Kavita");
πΉ Calling a Function Multiple Times
Functions can be reused anywhere in your program.
function printLine() {
console.log("---------------");
}
printLine();
console.log("JavaScript Functions");
printLine();
πΉ Functions Inside Loops
Functions work perfectly with loops.
function printNumber(num) {
console.log(num);
}
for (let i = 1; i <= 5; i++) {
printNumber(i);
}
β οΈ Common Mistakes
- Forgetting to call the function
- Using
console.loginstead ofreturn - Mismatch between parameters and arguments
- Writing too much code inside one function
π§ Practice Tasks
- Create a function to print your name
- Create a function to add two numbers
- Create a function to check even or odd
- Convert a normal function into an arrow function
- Use a function inside a loop
π§ͺ Mini Coding Challenge
Write a function:
function calculateBill(amount) {
}
Rules:
- If amount β₯ 1000 β give 10% discount
- Otherwise β no discount
- Return final amount
π Summary
- Functions are reusable blocks of code
- Parameters allow input
returnsends output back- Arrow functions are modern and concise
- Functions are the backbone of JavaScript & MERN
β‘οΈ Next Lesson
JavaScript Arrays (JS-06)
π Quiz & Badge
Quiz and badges will be enabled soon.