JavaScript Functions
JavaScript Functions
1. Introduction to Functions
📌 What is a Function?
A function is a reusable block of code that performs a specific task.
It helps in avoiding code repetition and makes programs modular.
💡 Example:
function greet() {
console.log("Hello, world!");
}
greet(); // Output: Hello, world!
2. Function Declaration (Named Function)
📌 Definition:
A function declared using the function keyword.
Can be called before declaration due to hoisting.
💡 Example:
function add(a, b) {
return a + b;
}
console.log(add(5, 3)); // Output: 8
3. Function Expression (Anonymous Function)
📌 Definition:
JavaScript Functions 1
A function assigned to a variable.
Not hoisted (must be defined before calling).
💡 Example:
const multiply = function(a, b) {
return a * b;
};
console.log(multiply(4, 2)); // Output: 8
4. Arrow Functions (ES6)
📌 Definition:
A shorter syntax using => .
No this binding.
💡 Example:
const square = (num) => num * num;
console.log(square(5)); // Output: 25
5. Parameters & Arguments
📌 Definition:
Parameters: Variables listed in function definition.
Arguments: Actual values passed when calling the function.
💡 Example:
function greet(name) { // name is a parameter
console.log("Hello, " + name);
}
greet("Alice"); // "Alice" is an argument
6. Default Parameters (ES6)
📌 Definition:
JavaScript Functions 2
Allows setting a default value for parameters.
💡 Example:
function greet(name = "Guest") {
console.log("Hello, " + name);
}
greet(); // Output: Hello, Guest
greet("Bob"); // Output: Hello, Bob
7. Rest Parameters (ES6)
📌 Definition:
Allows passing multiple arguments as an array.
💡 Example:
function sum(...numbers) {
return numbers.reduce((acc, num) => acc + num, 0);
}
console.log(sum(1, 2, 3, 4)); // Output: 10
8. Arguments Object (Pre-ES6)
📌 Definition:
arguments is an array-like object in traditional functions.
💡 Example:
function showArgs() {
console.log(arguments);
}
showArgs(10, 20, 30); // Output: [10, 20, 30]
🚨 Note: Not available in arrow functions.
9. Immediately Invoked Function Expressions (IIFE)
📌 Definition:
JavaScript Functions 3
A function that runs immediately after defining it.
💡 Example:
(function() {
console.log("IIFE executed!");
})(); // Output: IIFE executed!
Tasks On Functions
📝 Beginner Level (Tasks 1-10)
1️⃣ Write a function that takes a name as an argument and returns a greeting
message.
Example:
greet("Alice");
Output:
Hello, Alice!
2️⃣ Write a function that takes two numbers and returns their sum.
Example:
add(4, 7);
Output:
11
3️⃣ Write a function that checks if a number is even or odd.
Example:
isEven(5);
JavaScript Functions 4
Output:
Odd
4️⃣ Write a function that finds the square of a number.
Example:
square(6);
Output:
36
5️⃣ Write a function that takes an array of 5 numbers and returns their sum.
(Don't use built-in methods)
Example:
sumArray([2, 4, 6, 8, 10]);
Output:
30
6️⃣ Write a function that takes a number n and returns "Positive" , "Negative" , or
"Zero" .
Example:
checkNumber(-3);
Output:
Negative
7️⃣ Write a function that returns the factorial of a number using a loop.
Example:
JavaScript Functions 5
factorial(5);
Output:
120
8️⃣ Write a function to reverse a string without using built-in methods.
Example:
reverseString("hello");
Output:
olleh
9️⃣ Write a function that finds the largest number in an array of 5 numbers
(without using built-in methods).
Example:
findMax([12, 34, 7, 89, 23]);
Output:
89
🔟 Write a function that counts the number of vowels in a given string.
Example:
countVowels("hello");
Output:
📝 Intermediate Level (Tasks 11-15)
JavaScript Functions 6
1️⃣1️⃣ Write a function that takes any number of arguments and returns their
sum using rest parameters.
Example:
sum(1, 2, 3, 4, 5);
Output:
15
1️⃣2️⃣ Write an IIFE that logs "IIFE executed" to the console.
Output:
IIFE executed
📝 Advanced Level (Tasks 16-20)
1️⃣7️⃣ Write a function that returns the longest word in a sentence.
Example:
longestWord("This is a JavaScript function test");
Output:
JavaScript
1️⃣8️⃣ Write a function that takes an array and removes duplicates without using
built-in methods.
Example:
removeDuplicates([1, 2, 2, 3, 4, 4, 5]);
Output:
[1, 2, 3, 4, 5]
1️⃣9️⃣ Write a function using currying that adds numbers step by step.
JavaScript Functions 7
Example:
add(2)(3)(4);
Output:
2️⃣0️⃣ Write a function that checks if a string is a palindrome without using
built-in methods.
Example:
isPalindrome("racecar");
Output:
true
JavaScript Functions 8