JavaScript Functions
A function is a reusable block of code that performs a task. Functions help you organize your program, avoid repetition, and write clean, maintainable code.
What is a Function?
A function is a named block of code designed to perform a specific task. You define it once and call it whenever needed. Functions can accept inputs (parameters) and send back a result using return.
Types of Functions
Function Declaration
Traditional named function defined with the function keyword.
Function Expression
Function stored in a variable: const fn = function(){...}.
Arrow Function
Short form using =>. Great for callbacks.
Anonymous Function
A function without a name, usually passed as an argument.
Callback Function
A function passed to another function to be called later.
Built-in Function
Provided by JavaScript: alert, parseInt, etc.
Basic Features
- A function can accept zero or many parameters.
- Functions can return one value using
return. - Functions create their own scope — variables inside are private.
- Arrow functions are shorter but do not have their own
this.
Understanding JavaScript Functions
JavaScript functions are reusable blocks of code used to perform specific tasks. Functions help developers organize code, reduce repetition, and create modular applications.
What is a Function ?
In JavaScript, a function is a block of code that can be called whenever required. Functions may accept inputs called parameters and may return values.
Structure of Function
- Function Keyword : Used to declare a function.
- Function Name : Identifier used to call the function.
- Parameters : Inputs passed to the function.
- Function Body : Contains the executable code.
- Return Statement : Returns output from the function.
Function Syntax
// Declaration
function add(a, b) {
return a + b;
}
// Expression
const mul =
function(a, b){
return a * b;
};
// Arrow Function
const sub =
(a, b) => a - b;
// Call
add(2, 3);
Your First Function
<script>
function greet(name){
return
"Hello " +
name + "!";
}
document.write(
greet(
"PBA INSTITUTE"
)
);
</script>
Example 1 : Even or Odd
<!DOCTYPE html>
<html>
<body>
<script>
var n = 4;
check(n);
function check(n){
if(n % 2 == 0)
document.write(
"Even"
);
else
document.write(
"Odd"
);
}
</script>
</body>
</html>
Example 2 : Add Two Numbers
<script>
function add(a, b){
return a + b;
}
document.write(
"Sum = " +
add(8, 12)
);
</script>
Example 3 : Last Digit
<script>
var n = 123;
lastdigit(n);
function lastdigit(n){
var r =
n % 10;
document.write(r);
}
</script>
Example 4 : Maximum Number
<script> const max = (a,b) => (a > b ? a : b); document.write( "Max = " + max(40,25) ); </script>
Example 5 : Palindrome Number
<script>
var n = 121;
var p =
reverse(n);
if(p == n)
document.write(
"Palindrome"
);
else
document.write(
"Not Palindrome"
);
function reverse(n){
var s = 0;
while(n > 0){
r = n % 10;
s = s * 10 + r;
n =
parseInt(n / 10);
}
return s;
}
</script>
Example 6 : Default Parameter
<script>
function welcome(
name = "Guest"
){
return
"Welcome " +
name;
}
document.write(
welcome()
);
</script>
Example 7 : Factorial
<script>
function fact(n){
if(n <= 1)
return 1;
return
n * fact(n - 1);
}
document.write(
"5! = " +
fact(5)
);
</script>
Uses of Functions
- Code Reusability
- Modular Programming
- Better Organization
- Easier Maintenance
- Simplifies Complex Logic
Real Life Use Cases
Calculations
Used for tax, EMI and score calculation.
API Calls
Fetch data dynamically from servers.
Form Validation
Validate forms using reusable functions.
Frequently Asked Questions
| Question | Answer |
|---|---|
| What is hoisting? | Function declarations are moved to the top of their scope. |
| What are parameters? | Inputs passed to a function. |
| What is recursion? | A function calling itself repeatedly. |
Conclusion
Functions are one of the most important concepts in JavaScript programming. They help developers write clean, reusable, and modular code efficiently.
JavaScript All Chapters
Continue Learning
Previous
Go to Array Chapter