Interesting Facts about JavaScript Functions
Last Updated :
16 Jan, 2025
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();
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
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);
OutputThis 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
OutputHello, 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));
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!");
})();
OutputExecuted 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));
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);
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));
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!");
}
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));
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();
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
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
OutputHello!
Hi from a method!
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