Open In App

What is a Function in JavaScript ?

Last Updated : 29 Jan, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A function is a small block of code that will execute only when it is called or invoked in the code. The functions enhance the reusability of our code by preventing us from writing the same code again and again. We can simply call a function again in the code where we need a similar kind of code that is written in the body of a function. The execution of the code written in a function depends on us, as it will be executed only if it is called. Otherwise, it will not execute.

Example: The below code will explain how you can create and invoke a function in JavaScript.

JavaScript
// Function declaration
function GFG(){
    console.log("GeeksforGeeks is a Computer Science Portal.");
}

// Invoking a Function
// It will not execute without calling it.
GFG();

Output
GeeksforGeeks is a Computer Science Portal.

Next Article

Similar Reads

How to get the function name from within that function using JavaScript ?
Given a function and the task is to get the name of the function from inside the function using JavaScript. There are basically two methods to get the function name from within that function in JavaScript. These are: Using String substr() MethodUsing Function prototype name propertyGet the Function name using String substr() MethodThis method gets
2 min read
How to implement a function that enable another function after specified time using JavaScript ?
Let us assume that we have been given a function add() which takes in two parameters a and b, and returns their sum. We are supposed to implement a function that should be able to call any function after the given delay amount of time. This can be done with the approaches as given below: Approach 1: Create a function that takes in the amount of del
2 min read
How to create a function that invokes function with partials appended to the arguments in JavaScript ?
In this article, we will learn to create a function that invokes a function with partials appended to the arguments it receives in JavaScript. Approach: We want to implement a function that invokes another function and provides the arguments it received. We can get the result by using (...) the spread/rest operator. Explanation: Let's say, we creat
3 min read
How to create a function that invokes the provided function with its arguments transformed in JavaScript ?
In programming, functions are used to reduce the effort of writing the same instance of code repeatedly. In this article let us see how we can create a function that invokes the provided function with its arguments transformed in JavaScript. In this article, the function transformer invokes the function scaling with its arguments, where the functio
2 min read
How to create a function that invokes each provided function with the arguments it receives using JavaScript ?
In this article, we will see how to create a function that invokes each provided function with the arguments it receives using JavaScript. It helps in reducing the effort of creating the same instance of code again and again. In this article let us see how to create a function that invokes each provided function with the arguments it receives and r
3 min read
How to create a function that invokes function with partials prepended arguments in JavaScript ?
In this article, we will see how to create a function that invokes functions with partials prepended to the arguments it receives in JavaScript. Before understanding the problem statement and approaching the solution for the same, let's, first of all, know what a function (also called a method) is and how a function gets executed. A function (or a
5 min read
How does TypeScript support optional parameters in function as every parameter is optional for a function in JavaScript ?
Optional parameters are those parameters whose value may or may not be provided as an argument during the function call. Their value is set to undefined when they are not provided as an argument. It is different from a default parameter for which we need to provide a default value at the time of defining the function. This default value is used in
4 min read
Difference between "var functionName = function() {}" and "function functionName() {}" in JavaScript
In this article, we will be discussing the function functionName() {} and functionName = function() {} with suitable code examples for each condition & then we will see the difference between the function functionName() {} and functionName = function() {}. function functionName() {}: A function declaration is a statement that creates a named fu
3 min read
Difference between ‘function declaration’ and ‘function expression' in JavaScript
Functions in JavaScript allow us to carry out some set of actions, important decisions, or calculations and even make our website more interactive. In this article, we will learn the difference between ‘function declaration’ and ‘function expression’. The similarity is both use the keyword function and the most prominent difference is that the func
2 min read
How to call a function that return another function in JavaScript ?
The task is to call a function that returns another function with the help of JavaScript is called a Currying function, a function with numerous arguments that can be converted into a series of nesting functions with the help of the currying method. Approach:Define Outer Function:Create an outer function that takes parameters and contains the logic
2 min read
Difference between Function Declarations & Function Expressions in JavaScript
Function Declarations:Function declarations are defined using the function keyword followed by a name and a function body.They are hoisted to the top of their containing scope during the compilation phase, meaning they can be called before they are declared.Function declarations can be used to create named functions that can be called from anywhere
2 min read
Difference Between Function Overloading and Function Overriding in JavaScript
Function overloading and function overriding are two important concepts in object-oriented programming (OOP). Function overloading allows the creation of multiple functions with the same name but different parameters. Function overriding allows the subclass or child class to provide the specific implementation of the method that is already defined
3 min read
How to Check a Function is a Generator Function or not using JavaScript ?
Given an HTML document containing some JavaScript function and the task is to check whether the given function is generator function or not with the help of JavaScript. There are two examples to solve this problem which are discussed below: Example 1:In this example, we will use functionName.constructor.name property. It functionName.constructor.na
2 min read
Explore the concept of JavaScript Function Scope and different types of JavaScript Functions
JavaScript is based on functional programming. Therefore, functions are fundamental building blocks of JavaScript. So the function is called first-class citizens in JavaScript. Syntax: Define a functionfunction functionName(parameters) { // SET OF STATEMENTS } Call a functionfunctionName(arguments); The function execution stops when all the stateme
10 min read
How to Delay a JavaScript Function Call using JavaScript ?
Delaying a JavaScript function call involves postponing its execution for a specified time using methods like setTimeout(). This technique is useful for timed actions, animations, or asynchronous tasks, enabling smoother user experiences and better control over when certain operations run. There are several ways to delay the execution of a function
3 min read
Difference between Function.prototype.apply and Function.prototype.call
JavaScript treats everything as an object, even functions, and every object has its own properties and methods. Function objects have both apply() and call() methods on them. However, there is confusion about the two functions in JavaScript. The main difference between them is how they handle function arguments. There is no difference between these
2 min read
Explain the differences on the usage of foo between function foo() {} and var foo = function() {}
Here we see the differences on the usage of foo between function foo() {} and var foo = function() {} types of function declarations in JavaScript. function foo() {} is a Normal Function or traditional general way of Function Declaration which every user or developer finds it simpler to declare and use it every ever required and var foo = function(
4 min read
What are the advantages of synchronous function over asynchronous function in Node.js ?
Node.js furnishes us with an inbuilt fs (File System) module for different documents taking care of tasks like reading a record, composing a document, erasing a document, and so on. fs module can be introduced utilizing the underneath explanation: Syntax: npm introduce fs --save Note: The npm in the above order represents the hub bundle supervisor
5 min read
How the User-Defined Function differs from Built-in Function in PHP ?
In PHP, functions are blocks of reusable code that perform specific tasks. They enhance code readability, modularity, and maintainability by encapsulating logic into named units. PHP functions can be categorized into two main types: user-defined functions and built-in functions. User-Defined FunctionsUser-defined functions are functions created by
2 min read
JavaScript Math.round( ) function
JavaScript Math.round( ) function is used to round the number passed as a parameter to its nearest integer. Syntax: Math.round(value) Parameters: value: The number to be rounded to its nearest integer. Example 1: Rounding Off a number to its nearest integer To round off a number to its nearest integer, the math.round() function should be implemente
2 min read
JavaScript Math cbrt() Function
The Javascript Math.cbrt() function is used to find the cube root of a number. The cube root of a number is denoted as Math.cbrt(x)=y such that y^3=x. Syntax: Math.cbrt(number) Parameters: This function accepts a single parameter. number: The number whose cube root you want to know. Return Value: It returns the cube root of the given number. The be
1 min read
JavaScript Math.clz32() Function
The Math.clz32( ) function in JavaScript returns the number of leading zero bits in the 32-bit binary representation of a number. Clz32 stands for Count Leading Zeros 32. If the passed parameter is not a number, then it will be converted into a number first and then converted to a 32-bit unsigned integer. If the converted 32-bit unsigned integer is
2 min read
JavaScript Array join() Function
The JavaScript Array.join() method is used to join the elements of the array together into a string. The elements of the string will be separated by a specified separator and its default value is a comma(, ). Syntax: Array.join([separator]) Parameters: [separator]: A string to separate each element of the array. By default, the array elements are s
2 min read
JavaScript Array find() function
JavaScript arr.find() function is used to find the first element from the array that satisfies the condition implemented by a function. If more than one element satisfies the condition then the first element satisfying the condition is returned. Syntax: arr.find(function(element, index, array), thisValue) Arguments The argument to this function is
3 min read
JavaScript Array findIndex() Function
JavaScript arr.findIndex() function is used to find the index of the first element that satisfies the condition checked by the argument function from the given array. Suppose you want to find the index of the first element of the array that is even. Syntax: array.findIndex(function(element, index, arr), thisValue) Parameters: The argument to this f
3 min read
JavaScript Math.atanh() Function
The Javascript Math.atanh() function is an inbuilt function in JavaScript that is used to get the hyperbolic arctangent of a number. The hyperbolic arctangent is known by many names such as hyperbolic inverse tangent and atanh. It is the inverse of the hyperbolic tangent function i.e, The inverse hyperbolic tangent of any value says x is the value
2 min read
JavaScript escape() Function
The Javascript escape() function takes a string as a parameter and encodes it so that it can be transmitted to any computer in any network which supports ASCII characters. Note: escape(): This function was used to encode special characters in a string, but it has been replaced by the encodeURI() and encodeURIComponent() functions, which provide mor
2 min read
JavaScript date.@@toPrimitive() Function
The Javascript date.@@toPrimitive() function is an inbuilt function in JavaScript that is used to convert the date object into a primitive value. A number or string is known as a primitive value. Syntax: Dateobj[Symbol.toPrimitive](hint); Parameter: This function accepts a single parameter. Depending on the argument, the method can return either a
3 min read
Explain invoking function in JavaScript
In this article, we will learn about invoking the function in Javascript, along with understanding its implementation through examples. Function Invoking is a process to execute the code inside the function when some argument is passed to invoke it. You can invoke a function multiple times by declaring the function only once. When the function is d
2 min read
Passing a function as a parameter in JavaScript
In this article, we will pass a function as a parameter in JavaScript. Passing a function as an argument to the function is quite similar to passing a variable as an argument to the function. So variables can be returned from a function. The below examples describe passing a function as a parameter to another function. Example 1: This example passe
1 min read
  翻译: