Functions — Complete Guide
Functions — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of JavaScript Tutorial on Toolliyo Academy.
On this page
JavaScript Tutorial · Lesson 14 of 100
Functions
Basics → Objects & data → Async & DOM → Advanced → Tools → Projects
Beginner · 1 — Learn by example · ~6 min · JS Control Flow & Functions
What is this?
A function is a reusable block of code with a name. You define it once and call it whenever you need that work done — calculate tax, format a date, validate email.
Why should you care?
Without functions you copy-paste the same logic everywhere. One bug fix then means editing ten places.
See it live — copy this example
Paste into an HTML file or the browser console (F12). Use Run below when the live editor is available.
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Sandeep"));
console.log(greet("Anita"));
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- function greet(name) declares a function with one parameter.
- return sends a value back to the caller.
- greet("Sandeep") calls the function with an argument.
Practice next
- Run greet with different names.
- Add function add(a, b) { return a + b; } and test it.
- Call greet() with no argument and note undefined.
- Give greet a default parameter: name = "Guest".
- Return an object from a function that builds a user profile.
Remember
function name(params) { ... return value; } Call with name(arg) Parameters are placeholders; arguments are real values
ScriptVerse invoice formatter
formatCurrency(amount) is reused in cart, receipts, and admin reports instead of duplicating formatting logic.
Outcome: Functions centralize business rules so one fix updates every screen.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!