By creating your own functions in JavaScript, you can create reusable code and achieve efficient development. In this article, we will explain in detail how to create your own functions.
I want to create my own function, what should I do?
Let’s explain the steps to create your own function in JavaScript.
Understand the basics of self-made functions
In JavaScript, self-created functions are functions that users can define and use on their own .
Creating these functions requires the following syntax:
function function(Argument1, Argument2, ...){
Processed;
return value;.
}
The function name can be anything you like, but it must be unique. Arguments are optional, but you can receive variable values for use within the function.
The processing content is what is executed when the function is called. You can also return a return value from a function by using the return statement.
Regarding function scope, variables defined within a function are valid only within the function and cannot be accessed from outside. This is because variables defined within a function are treated as local variables.
How to call your own function
You can execute your own functions by calling them using the function name. If arguments are required, they can be specified when calling. Also, if you use the return statement, you can use the value returned from the function.
Below is a sample program that uses custom functions.
function add(a, b){
return a + b;
}
let result = add(1, 2);
console.log(result); // 3
This sample program defines the add function. This function takes two arguments and returns the result of adding them together.
About types of functions
Function types include named functions , anonymous functions , and arrow functions .
named function
Named functions are a way to name and define functions . Named functions can be used as reusable code blocks.
function function(argument) {
Processed;
}
In the sample below, the add function is defined. This function takes two arguments and returns the result of adding them together.
function add(a, b) {
return a + b;
}
console.log(add(1, 2)); // 3
anonymous function
An anonymous function is a way to define a function without a name . Anonymous functions are often used for temporary processing or to be passed as arguments.
var variable = function (argument) {
Processed;
}
In the sample below, an anonymous function is assigned to the variable add. This function takes two arguments and returns the result of adding them together.
var add = function (a, b) {
return a + b;
};
console.log(add(1, 2)); // 3
arrow function
Arrow functions are a new way of defining functions introduced in ES6. Arrow functions are characterized by short code and clean writing .
const variable = (argument) => {
Processed;
}
In the sample below, the arrow function takes arguments ‘a’ and ‘b’ and adds them together. Return the result using the “return” statement.
const add = (a, b) => {
return a + b;
};
console.log(add(1, 2)); // 3
Summary of this article
I explained about self-made functions.
- Homebrew functions are reusable code blocks
- To create a function, use the “function” keyword.
- You can create functions that accept arguments and perform processing.
- Functions that have a return value are written using “return”.
- There are various types of self-written functions, such as named, anonymous, and arrow functions.
By creating your own functions, you can improve the visibility of your code and make development more efficient.
Choose a function name that is easy to understand, and choose a name that matches the processing content.
Comments