JavaScript Function Examples Last Updated : 05 Aug, 2025 Comments Improve Suggest changes Like Article Like 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.JavaScript Function ExamplesSyntax: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); Output7 JavaScript Function ExamplesThe following JavaScript section contains a wide collection of JavaScript examples. Many of these programming examples contain multiple approaches to solve the problem.Sr. No.QuestionsHow to write a function in JavaScript?2What is currying function in JavaScript?3How to call JavaScript function in HTML?4What is the purpose of setTimeout() function in JavaScript?5What is a typical use case for anonymous functions in JavaScript?6How to check whether a number is NaN or finite in JavaScript?7How to encode and decode a URL in JavaScript?8How to declare the optional function parameters in JavaScript?9How to get the javascript function parameter names/values dynamically?10How to include a JavaScript file in another JavaScript file?11How to override a JavaScript function?12Using the function* Declaration in JavaScript13What is the difference between call and apply in JavaScript?14How to find out the caller function in JavaScript?15How to negate a predicate function in JavaScript?16How to iterate over a callback n times in JavaScript?17How to create a function that invokes function with partials appended to the arguments in JavaScript?18How to remove the key-value pairs corresponding to the given keys from an object using JavaScript?19How to create a function that invokes each provided function with the arguments it receives using JavaScript?20How to call function from it name stored in a string using JavaScript?21How to measure time taken by a function to execute using JavaScript?22How to check a function is defined in JavaScript?23How to create a function that invokes the provided function with its arguments transformed in JavaScript?24How to wait for a promise to finish before returning the variable of a function?25How to change the value of a global variable inside of a function using JavaScript?26How to get the index of the function in an array of functions which executed the fastest in JavaScript?27How to add an element horizontally in Html page using JavaScript?28How to add fade-out effect using pure JavaScript?29How to add fade-in effect using pure JavaScript?30How 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 Comment More info V vishalkumar2204 Follow Improve Article Tags : JavaScript Web Technologies Explore JavaScript BasicsIntroduction to JavaScript4 min readJavaScript Versions2 min readHow to Add JavaScript in HTML Document?3 min readJavaScript Syntax6 min readJavaScript Output4 min readJavaScript Comments2 min readVariables & DatatypesVariables and Datatypes in JavaScript6 min readGlobal and Local variables in JavaScript4 min readJavaScript Let6 min readJavaScript const5 min readJavaScript Var Statement7 min readOperatorsJavaScript Operators5 min readOperator precedence in JavaScript2 min readJavaScript Arithmetic Operators5 min readJavaScript Assignment Operators5 min readJavaScript Comparison Operators5 min readJavaScript Logical Operators5 min readJavaScript Bitwise Operators5 min readJavaScript Ternary Operator4 min readJavaScript Comma Operator2 min readJavaScript Unary Operators4 min readJavaScript in and instanceof operators3 min readJavaScript String Operators3 min readStatementsJavaScript Statements4 min readJavaScript if-else3 min readJavaScript switch Statement4 min readJavaScript Break Statement2 min readJavaScript Continue Statement1 min readJavaScript Return Statement4 min readLoopsJavaScript Loops3 min readJavaScript For Loop4 min readJavaScript While Loop3 min readJavaScript For In Loop3 min readJavaScript for...of Loop3 min readJavaScript do...while Loop4 min readPerformance & DebuggingJavaScript | Performance4 min readDebugging in JavaScript4 min readJavaScript Errors Throw and Try to Catch2 min readObjectsObjects in Javascript4 min readObject Oriented Programming in JavaScript3 min readJavaScript Objects6 min readCreating objects in JavaScript5 min readJavaScript JSON Objects3 min readJavaScript Object Reference4 min readFunctionsFunctions in JavaScript5 min readHow to write a function in JavaScript ?4 min readJavaScript Function Call2 min readDifferent ways of writing functions in JavaScript3 min readDifference between Methods and Functions in JavaScript3 min readExplain the Different Function States in JavaScript3 min readJavaScript Function Complete Reference3 min readArraysJavaScript Arrays7 min readJavaScript Array Methods7 min readBest-Known JavaScript Array Methods6 min readImportant Array Methods of JavaScript7 min readJavaScript Array Reference4 min readStringJavaScript Strings6 min readJavaScript String Methods9 min readJavaScript String Reference4 min read Like