⬅ Previous Topic
JavaScript do while Loop Explained with ExamplesNext Topic ⮕
JavaScript Function Expressions⬅ Previous Topic
JavaScript do while Loop Explained with ExamplesNext Topic ⮕
JavaScript Function ExpressionsIn JavaScript, functions are reusable blocks of code designed to perform a specific task. They're the building blocks of any application – small or large. Functions help break complex problems into manageable parts and make your code modular, readable, and reusable.
You can define a function using the function
keyword followed by a name, parentheses ()
, and a block of code inside curly braces {}
.
function greet() {
console.log("Hello, welcome to JavaScript!");
}
After defining the function, you must call or invoke it using its name followed by parentheses.
greet();
Hello, welcome to JavaScript!
You can pass data into functions through parameters, and use arguments when calling them.
function greetUser(name) {
console.log("Hello, " + name + "!");
}
greetUser("Asha");
Hello, Asha!
The return
keyword sends a value back to the place where the function was called.
function add(a, b) {
return a + b;
}
let result = add(5, 3);
console.log(result);
8
Functions can be assigned to variables. These are called function expressions.
const multiply = function(a, b) {
return a * b;
};
console.log(multiply(4, 2));
8
Arrow functions provide a shorter syntax and lexically bind the this
value.
const divide = (a, b) => {
return a / b;
};
console.log(divide(10, 2));
5
When there's a single return statement, you can make it more concise:
const square = x => x * x;
console.log(square(6));
36
You can assign default values to parameters.
function greet(name = "Guest") {
console.log("Hello, " + name);
}
greet(); // Uses default
greet("Ravi"); // Uses given argument
Hello, Guest
Hello, Ravi
When you don't know how many arguments you'll receive, use the rest operator ...
.
function sum(...numbers) {
return numbers.reduce((acc, curr) => acc + curr, 0);
}
console.log(sum(1, 2, 3, 4));
10
A callback is a function passed as an argument to another function and executed later.
function process(callback) {
console.log("Processing...");
callback();
}
function done() {
console.log("Done!");
}
process(done);
Processing...
Done!
IIFEs are functions that execute immediately after definition.
(function() {
console.log("IIFE executed!");
})();
IIFE executed!
Functions define their own scope. Variables declared inside a function are local and not accessible outside.
function showScope() {
let message = "Inside function";
console.log(message);
}
showScope();
console.log(message); // Error
Inside function
Uncaught ReferenceError: message is not defined
A closure gives you access to an outer function’s variables even after the outer function has finished executing.
function outer() {
let count = 0;
return function inner() {
count++;
console.log("Count: " + count);
};
}
const counter = outer();
counter();
counter();
counter();
Count: 1
Count: 2
Count: 3
Pure functions always return the same output for the same input and have no side effects.
// Pure
function pureAdd(a, b) {
return a + b;
}
Impure functions may change external state or depend on it.
// Impure
let counter = 0;
function impureAdd() {
counter++;
return counter;
}
Write a function called greetTime(name, time)
that returns:
This will help you practice if-else
and string concatenation inside a function!
⬅ Previous Topic
JavaScript do while Loop Explained with ExamplesNext Topic ⮕
JavaScript Function ExpressionsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.