Open In App

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

Similar Reads