DEV Community

Cover image for Basic Functions in Javascript
Madhan Raj
Madhan Raj

Posted on

Basic Functions in Javascript

Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.


Understanding Functions

In functions, parameters are placeholders defined in the function, while arguments are the actual values you pass when calling the function.
Example:

function greet(name) {   // 'name' is a parameter
  console.log("Hello " + name);
}

greet("Alice");  // "Alice" is the argument
Enter fullscreen mode Exit fullscreen mode
Parameter: name (placeholder inside the function).
Argument: "Alice" (real value given at call time).
Enter fullscreen mode Exit fullscreen mode

Default Parameters
Default parameters are used when no argument is provided during the function call.
If no value is passed, the function automatically uses the default value.

function greet(name = "Guest") {
  console.log("Hello, " + name);
}

greet();
greet("Aman");
Enter fullscreen mode Exit fullscreen mode

Return Statement
The return statement is used to send a result back from a function.
When return executes, the function stops running at that point.
The returned value can be stored in a variable or used directly.

function add(a, b) {
  return a + b; // returns the sum
}

let result = add(5, 10);
console.log(result);
Enter fullscreen mode Exit fullscreen mode

JavaScript Function Parameters

Parameters (Function Input)

Parameters allow you to pass (send) values to a function.

Parameters are listed inside the parentheses in the function definition.
Example

function multiply(a, b) {
  return a * b;
}

let result = multiply(4, 5);
Enter fullscreen mode Exit fullscreen mode

Default Parameter Values
ECMAScript 2015 allows function parameters to have default values.

The default value is used if no argument is provided.
Example
If y is not passed or undefined, then y = 10.

function myFunction(x, y = 10) {
  return x + y;
}
myFunction(5);
Enter fullscreen mode Exit fullscreen mode

Parameters vs. Arguments

In JavaScript, function parameters and arguments are distinct concepts:

Parameters are the names listed in the function definition.

Arguments are the real values passed to, and received by the function.

JavaScript Function Return
A function can return a value back to the code that called it.

The return statement is used to send a value out of a function.
Returning Values from Functions
The return Statement
When a function reaches a return statement, the function stops executing.

The value after the return keyword is sent back to the caller.
Example

function sayHello() {
  return "Hello World";
}

let message = sayHello();
Enter fullscreen mode Exit fullscreen mode

Returning a Calculated Value
Most functions return the result of a calculation or an operation.

Example

function multiply(a, b) {
  return a * b;
}

let result = multiply(4, 5);
Enter fullscreen mode Exit fullscreen mode

Reference
https://www.w3schools.com/js/js_function_arguments.asp

Top comments (0)