Functions in JavaScript – Definition, Parameters, Return Values, and Structural Variations

Functions are central to JavaScript program structure. This article explores function declarations and expressions, how parameters and return values work, and how functions behave as assignable values. It also covers defining functions as object properties and compares them to class-based syntax. Understanding these forms is key to writing flexible, maintainable code.

functionparameterreturnexpression

~3 min read · Updated Oct 14, 2025

Introduction


In programming, the term “function” can have different meanings. In Functional Programming (FP), it has a strict mathematical definition. But in JavaScript, a function is more broadly understood as a procedure: a set of statements that can be invoked multiple times, may receive inputs, and may return outputs.


Function Declaration


From the early days of JS, functions have been defined like this:


function awesomeFunction(coolThings) {
  // ...
  return amazingStuff;
}

This is a function declaration because it appears as a standalone statement. The association between the name awesomeFunction and the function value happens during the compile phase, before execution.


Function Expression


Functions can also be defined as expressions and assigned to variables:


var awesomeFunction = function(coolThings) {
  // ...
  return amazingStuff;
};

Unlike declarations, function expressions are associated with their identifiers at runtime, not during compilation.


Functions as Values


In JavaScript, functions are values — they can be assigned to variables, passed as arguments, and stored in data structures. JS functions are a special type of object, which enables functional programming patterns.


Parameters and Input


Functions can receive input via parameters:


function greeting(myName) {
  console.log(`Hello, ${myName}!`);
}
greeting("Kyle"); // Hello, Kyle!

myName is a parameter that acts as a local variable inside the function. You can define any number of parameters, and each receives the corresponding argument from the function call.


Returning Values


Functions can return values using the return keyword:


function greeting(myName) {
  return `Hello, ${myName}!`;
}
var msg = greeting("Kyle");
console.log(msg); // Hello, Kyle!

Only one value can be returned directly, but multiple values can be wrapped in an array or object.


Functions as Object Properties


Because functions are values, they can be assigned as properties on objects:


var whatToSay = {
  greeting() {
    console.log("Hello!");
  },
  question() {
    console.log("What's your name?");
  },
  answer() {
    console.log("My name is Kyle.");
  }
};
whatToSay.greeting(); // Hello!

Here, three functions are defined as properties of the whatToSay object. Each can be called by accessing the property name. This style is simpler than class-based syntax, which is covered later in the book.


Conclusion


Functions in JavaScript are powerful and versatile. They can be declared or expressed, accept parameters, return values, and be stored as object properties. Understanding these forms and behaviors is essential for writing clean, reusable, and expressive code.


Written & researched by Dr. Shahin Siami

Related Articles

Coercive Conditional Comparison and Prototypal Classes in JavaScript – Understanding Implicit Logic and Legacy Inheritance Patterns

This article explores two advanced topics from Appendix A of You Don’t Know JS Yet: how JavaScript performs coercive comparisons in conditional expressions, and how prototypal class patterns were used before ES6 introduced the class keyword. Through practical examples, it clarifies how implicit boolean logic works and how prototype chains enable behavior delegation.

Continue

Value vs Reference and the Many Forms of Function Definitions

Value vs Reference and the Many Forms of Function Definitions

Continue

Prototypes in JavaScript – Delegation, Prototype Chains, and Dynamic this Behavior

Prototypes in JavaScript provide a mechanism for property delegation between objects. This article explains how prototype chains work, how to create linked objects using Object.create, and how property access and assignment behave in relation to delegation. It also explores how the dynamic nature of this enables prototype-based method reuse across multiple objects.

Continue

Closure and this in JavaScript – Scope Memory and Dynamic Execution Context

Closure and this are two foundational and often misunderstood concepts in JavaScript. This article defines closure as a function’s ability to remember variables from its outer scope, and explains how this refers to the dynamic execution context of a function call. Through practical examples, it clarifies the difference between static scope and dynamic context.

Continue

Iteration in JavaScript – The Iterator Pattern, Built-in Iterables, and Standard Data Consumption

Iteration is a foundational pattern for processing data in JavaScript. This article explores the iterator protocol, how to consume iterators using for..of loops and the spread operator, the distinction between iterators and iterables, and how built-in structures like arrays, strings, maps, and sets support standardized iteration. It also shows how to customize iteration for your own data structures.

Continue

Modules in JavaScript – From Classic Patterns to ES Modules and Structural Differences

Modules in JavaScript, like classes, are designed to group data and behavior into logical units. This article explores the classic module pattern using factory functions, compares it to class-based design, and introduces ES Modules (ESM) introduced in ES6. It explains how modules are defined, exported, imported, and instantiated, and highlights the differences in structure and usage.

Continue