Open In App

JavaScript Function Examples

Last Updated : 30 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A function in JavaScript is a set of statements that perform a specific task. It takes inputs, and performs computation, and produces output. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can call that function.

Syntax:

function myFun(parameter) {
    statements...
};

Example: Here is the basic JavaScript function example in which we add 2 elements.

Javascript




function myFunction(a, b) {
    return a + b;
}
  
// Calling the function
const result = myFunction(5, 2);
console.log(result);


Output

7

JavaScript Function Examples

The following JavaScript section contains a wide collection of JavaScript examples. Many of these programming examples contain multiple approaches to solve the problem.

Sr. No.

Questions

How to write a function in JavaScript?
2 What is currying function in JavaScript?
3 How to call JavaScript function in HTML?
4 What is the purpose of setTimeout() function in JavaScript?
5 What is a typical use case for anonymous functions in JavaScript?
6 How to check whether a number is NaN or finite in JavaScript?
7 How to encode and decode a URL in JavaScript?
8 How to declare the optional function parameters in JavaScript?
9 How to get the javascript function parameter names/values dynamically?
10 How to include a JavaScript file in another JavaScript file?
11 How to override a JavaScript function?
12 Using the function* Declaration in JavaScript
13 What is the difference between call and apply in JavaScript?
14 How to find out the caller function in JavaScript?
15 How to negate a predicate function in JavaScript?
16 How to iterate over a callback n times in JavaScript?
17 How to create a function that invokes function with partials appended to the arguments in JavaScript?
18 How to remove the key-value pairs corresponding to the given keys from an object using JavaScript?
19 How to create a function that invokes each provided function with the arguments it receives using JavaScript?
20 How to call function from it name stored in a string using JavaScript?
21 How to measure time taken by a function to execute using JavaScript?
22 How to check a function is defined in JavaScript?
23 How to create a function that invokes the provided function with its arguments transformed in JavaScript?
24 How to wait for a promise to finish before returning the variable of a function?
25 How to change the value of a global variable inside of a function using JavaScript?
26 How to get the index of the function in an array of functions which executed the fastest in JavaScript?
27 How to add an element horizontally in Html page using JavaScript?
28 How to add fade-out effect using pure JavaScript?
29 How to add fade-in effect using pure JavaScript?
30 How to understand various snippets of setTimeout() function in JavaScript?

We have a complete list of Javascript Function, to check those please go through this JavaScript Function Complete Reference article.

We have created an article of JavaScript Examples, and added list of questions based on Array, Object, Function, …, etc. Please visit this JavaScript Examples to get complete questions based articles list of JavaScript.

We have created a complete JavaScript Tutorial to help both beginners and experienced professionals. Please check this JavaScript Tutorial to get the complete content from basic syntax and data types to advanced topics such as object-oriented programming and DOM manipulation.

Recent articles on JavaScript



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads