Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
Interesting Facts about JavaScript Functions
Next article icon

Interesting Facts about JavaScript Functions

Last Updated : 16 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Let us talk about some interesting facts about JavaScript Functions that can make you an efficient programmer.

Functions Are First-Class Citizens

JavaScript treats functions as first-class citizens, meaning they can be:

  • Stored in variables
  • Passed as arguments to other functions
  • Returned from other functions

This flexibility makes JavaScript functions a powerful tool for functional programming.

JavaScript
const greet = () => console.log("Hello, World!"); const sayHi = greet;   sayHi(); 

Output
Hello, World! 

Functions Can Be Anonymous

JavaScript allows functions without a name, referred to as anonymous functions. These are especially useful in callbacks.

JavaScript
setTimeout(function() {       console.log("Executed after 2 seconds!");   }, 2000); 

Closures: Functions Remember Their Scope

Functions in JavaScript form closures, which means they retain access to their lexical scope even when executed outside of it.

JavaScript
function createCounter() {       let count = 0;       return function() {           count++;           console.log(count);       };   } const counter = createCounter();   //Driver Code Starts counter();   counter(); //Driver Code Ends 

Output
1 2 

Functions Are Objects

In JavaScript, functions are a special type of object, with properties like name and length. You can also dynamically add properties to functions.

JavaScript
function example() {}   example.info = "This is a function object";   console.log(example.info); 

Output
This is a function object 

Default Parameters

JavaScript supports default parameter values, simplifying the creation of functions with optional parameters.

JavaScript
function greet(name = "Guest") {       console.log(`Hello, ${name}!`);   }   //Driver Code Starts greet();   greet("Meeta"); //Driver Code Ends 

Output
Hello, Guest! Hello, Meeta! 

Arrow Functions Simplify Syntax

Arrow functions provide a concise way to define functions and automatically bind this to their enclosing context.

JavaScript
const add = (a, b) => a + b;   console.log(add(5, 3)); 

Output
8 

Functions Can Be Immediately Invoked

JavaScript allows functions to be executed immediately upon definition using an Immediately Invoked Function Expression (IIFE).

JavaScript
(function() {       console.log("Executed immediately!");   })(); 

Output
Executed immediately! 

Rest Parameters

Rest parameters allow a function to accept an indefinite number of arguments as an array.

JavaScript
function sum(...numbers) {       return numbers.reduce((acc, num) => acc + num, 0);   } console.log(sum(1, 2, 3, 4)); 

Output
10 

Functions Can Be Used as Constructors

In JavaScript, functions can be used to create objects using the new keyword.

JavaScript
function Person(name) {       this.name = name;   } const person = new Person("Meeta");   console.log(person.name); 

Output
Meeta 

Recursive Functions

JavaScript functions can call themselves, enabling recursion for problems like factorials or tree traversals.

JavaScript
function factorial(n) {       return n <= 1 ? 1 : n * factorial(n - 1);   } console.log(factorial(5)); 

Output
120 

Hoisting of Function Declarations

Function declarations are hoisted, meaning they can be called before they are defined in the code.

JavaScript
sayHello();   function sayHello() {       console.log("Hello, World!");   } 

Output
Hello, World! 

Higher-Order Functions

Functions that take other functions as arguments or return them are called higher-order functions.

JavaScript
function calculate(a, b, operation) {       return operation(a, b);   } console.log(calculate(5, 3, (x, y) => x + y)); 

Output
8 

Functions Are Dynamic

In JavaScript, you can dynamically modify or assign new functionality to an existing function.

JavaScript
function greet() {       console.log("Hello!");   }   greet();   greet = function() {       console.log("Hi there!");   };   greet(); 

Output
Hello! Hi there! 

Currying Functions

Currying is a functional programming technique where a function takes one argument at a time and returns another function to handle the next argument.

JavaScript
function multiply(a) {       return function(b) {           return a * b;       };   }   //Driver Code Starts const double = multiply(2);   console.log(double(5));   console.log(multiply(3)(4)); //Driver Code Ends 

Output
10 12 

Functions Can Have Their Own Methods

As functions are objects, you can attach methods to them just like you would with objects.

JavaScript
function greet() {       console.log("Hello!");   }   greet.sayHi = function() {       console.log("Hi from a method!");   };    //Driver Code Starts greet();   greet.sayHi(); //Driver Code Ends 

Output
Hello! Hi from a method! 

Next Article
Interesting Facts about JavaScript Functions

M

meetahaloyx4
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-functions

Similar Reads

    Interesting Facts About JavaScript
    JavaScript (often abbreviated as JS) is one of the most popular programming languages in the world. It is an interpreted, high-level programming language that follows ECMAScript. It powers interactive websites and is packed with amazing features that make it special and powerful. Interesting Facts A
    5 min read
    Arrow functions in JavaScript
    An arrow function is a shorter syntax for writing functions in JavaScript. Introduced in ES6, arrow functions allow for a more concise and readable code, especially in cases of small functions. Unlike regular functions, arrow functions don't have their own this, but instead, inherit it from the surr
    5 min read
    All about Functions and Scopes in JavaScript
    In this article, we will cover all the basic concepts of JS functions, callbacks, scopes, closures in-depth which would help you to - understand different types of function declarations.make better use of functions.understand how different scopes and scope chain works in JS.learn about closures and
    10 min read
    Functions in JavaScript
    Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9));Output15 Function Syntax and W
    5 min read
    JavaScript Course Functions in JavaScript
    Javascript functions are code blocks that are mainly used to perform a particular function. We can execute a function as many times as we want by calling it(invoking it). Function Structure: To create a function, we use function() declaration. // Anonymous function function(){ // function...body } /
    4 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