#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Functions - Level I

A function is a named, reusable block of code that performs one task. You define it once and run it whenever you need that task done, optionally giving it input values and getting a result back. This first level of functions covers the essentials every JavaScript program relies on: how to create functions, call them, pass data in, and get data out.

Why programs are built from functions

Without functions, the same lines of code get copied wherever a task repeats, for example calculating tax on every product. When the tax rule changes, you have to find and fix every copy. With a function, you fix one place.

Functions also give a name to an idea. A line like calculateTax(price) tells the reader what happens without forcing them to read the arithmetic. Breaking a big problem into small, well-named functions is one of the most valuable habits a programmer can build.

Anatomy of a function

// declaration: name, parameters, body
function calculateTax(amount, rate = 0.18) {
  const tax = amount * rate;
  return Math.round(tax * 100) / 100;
}

// calling it with arguments
const gst = calculateTax(1200);          // 216
const reduced = calculateTax(1200, 0.05); // 60

console.log(`Total: ${1200 + gst}`);
  • Name: calculateTax. Use a verb that says what the function does.
  • Parameters: amount and rate, placeholders listed in the brackets. rate = 0.18 gives a default value when no argument is supplied.
  • Body: the statements inside the curly braces.
  • Return value: whatever follows return is sent back to the caller. A function with no return gives back undefined.

Ways to create a function

FormExampleNotes
Declarationfunction greet(n) { ... }Hoisted, so it can be called before the line where it appears.
Expressionconst greet = function (n) { ... };Usable only after the assignment runs.
Arrow functionconst greet = (n) => `Hi ${n}`;Short syntax; a single expression is returned automatically. Has no own this.

All three create the same kind of value. Choosing between them is mostly about style and about how this should behave, which matters more once you work with objects.

Defining is not running

The most common beginner confusion is writing a function and wondering why nothing happens. A definition only describes the task; the code inside runs when you call the function by writing its name followed by brackets, like calculateTax(500).

The opposite slip is just as common: writing calculateTax without brackets. That refers to the function itself rather than running it, so console.log(calculateTax) prints the function's code instead of a number. Both behaviours are useful in the right place, as you will see with callbacks and event handlers.

Good function habits from the start

  • Keep each function focused on one job. If you need "and" to describe it, consider splitting it.
  • Prefer returning a value over printing inside the function. A function that returns can be reused anywhere; one that only logs cannot.
  • Pass in what the function needs through parameters instead of reading outside variables, so its behaviour is predictable.

What this level covers next

The lessons that follow go deeper into each piece introduced here: the function definition itself, how values are passed to parameters, how return values work, how local and global variables differ, and how functions behave as objects in JavaScript.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course