Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Javascript Map Programming Examples
Next article icon

JavaScript Function Examples

Last Updated : 11 Jul, 2024
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-Examples
JavaScript Function Examples

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?
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 JavaScript
13What 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


Next Article
Javascript Map Programming Examples

V

vishalkumar2204
Improve
Article Tags :
  • JavaScript
  • Web Technologies

Similar Reads

  • JavaScript Programming Examples
    JavaScript is a dynamic, widely-used programming language that plays a pivotal role in modern web development. Whether you're a beginner or an experienced developer, practicing JavaScript programming examples is an excellent way to refine your coding skills, enhance your problem-solving abilities, a
    15 min read
  • JavaScript Array Examples
    JavaScript array is used to store multiple elements in a single variable. [GFGTABS] JavaScript let a = ["HTML", "CSS", "JS"] console.log(a) [/GFGTABS]Output[ 'HTML', 'CSS', 'JS' ] Using New Keyword [GFGTABS] JavaScript // Declaring an array using Array Constructor let a
    3 min read
  • JavaScript Date Programming Examples
    The JavaScript comes with a built-in datatype called the Date object. The Date object is created by using a new keyword, i.e. new Date() . The Date object can be used for date and time in terms of millisecond precision within 100 million days before or after 1/1/1970. This article contains a wide co
    5 min read
  • JavaScript Function Examples
    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
    3 min read
  • Javascript Map Programming Examples
    A Javascript map is a collection of elements where each element is stored as a Key, value pair. Map objects can hold both objects and primitive values as either key or value. When we iterate over the map object it returns the key, and value pair in the same order as inserted. Syntax: let map = new M
    3 min read
  • JavaScript Math Programming Examples
    The JavaScript Math Object is used to perform mathematical operations on numbers. The Math object does not have a constructor. All properties and methods of Math are fixed/static. All properties and methods of Math are static and can be called by using Math as an object. Syntax: Math.propertyExample
    2 min read
  • JavaScript Number Programming Examples
    Javascript Numbers are primitive data types. Unlike other programming languages, Both integers and floating-point values are represented by JavaScript's number type. The IEEE-754 format is the one that is technically used by the JavaScript number type. Here, you don’t need int, float, etc, to declar
    3 min read
  • JavaScript Object Programming Examples
    JavaScript Object is the collection of properties and the property is in key, value pair format. These objects are quite different from JavaScript’s primitive data types (Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data types all store a single value each
    6 min read
  • JavaScript Set Programming Examples
    In JavaScript, the Set is a built-in collection that allows you to store unique values of any type, Set can store any type of value whether primitive or objects. It provides methods for adding, removing, and querying elements in an efficient way. [GFGTABS] JavaScript let s = new Set(["a",
    1 min read
  • JavaScript String Exercise
    JavaScript string is a primitive data type and is used for storing and manipulating a sequence of characters. It can contain zero or more characters within single or double quotes. This article contains a wide collection of JavaScript Programming examples based on String. [GFGTABS] JavaScript let s
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences