#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

Function definition

Defining a function means creating a reusable block of code with a name (or stored in a variable) that you can run whenever you need it. JavaScript offers several ways to define functions: declarations, expressions, arrow functions and methods. They mostly do the same job but differ in hoisting, how this works, and how concise they are.

Function declarations

A declaration starts with the function keyword, followed by a name, a list of parameters in brackets, and a body in curly braces. Declarations are hoisted: the whole function is available before the line where it appears, so you can call it near the top of a file and define it lower down.

Function expressions and arrow functions

A function expression creates a function as a value and assigns it to a variable. Arrow functions are a shorter expression syntax introduced in ES2015. When an arrow function has a single expression as its body, you can drop the braces and the return keyword, and the value is returned automatically.

// 1. Declaration (hoisted)
console.log(area(3, 4));   // works: 12
function area(width, height) {
  return width * height;
}

// 2. Function expression
const perimeter = function (width, height) {
  return 2 * (width + height);
};

// 3. Arrow functions
const square = (n) => n * n;
const toUser = (name) => ({ name, active: true }); // returning an object
const logAll = (...items) => {
  items.forEach((item) => console.log(item));
};

// 4. Default parameter values
function greet(name = "friend") {
  return `Hello, ${name}`;
}
console.log(greet());       // "Hello, friend"

Comparing the styles

StyleHoisted?Own this?Good for
DeclarationYesYesMain, named helper functions
ExpressionNoYesFunctions created conditionally or passed around
ArrowNoNo, uses surrounding thisCallbacks, short transformations
Method shorthandNoYesFunctions inside objects and classes

Parameters are not arguments

Parameters are the names listed in the definition; arguments are the actual values passed when calling. JavaScript does not check that the counts match. Missing arguments become undefined unless you give a default value, and extra arguments are ignored unless you collect them with a rest parameter such as ...items.

Pitfalls in definitions

Calling a function expression before its line throws an error. With const, the variable exists but is in the "temporal dead zone", so you get "Cannot access 'perimeter' before initialization". Only declarations can be used early.

Returning an object from a concise arrow function is another trap. Writing (name) => { name } makes JavaScript read the braces as a function body, so it returns undefined. Wrap the object in parentheses, as in toUser above.

Finally, a return with the value on the next line returns undefined, because JavaScript inserts a semicolon after return. Keep the value on the same line, or start a parenthesis on that line.

Naming well

Name functions after what they do, usually starting with a verb: calculateTotal, formatDate, isValidEmail. A good name makes the call site read like a sentence and often removes the need for a comment.

Keep each function focused on one job. If you need the word "and" to describe what it does, it can probably be split into two smaller functions.

Takeaway: use declarations for main named functions, arrow functions for short callbacks, give parameters sensible defaults, and wrap object literals in parentheses when returning them from concise arrows.

Ready to master real-world web development?

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

Explore Course