Interesting Facts about JavaScript Functions
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.
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.
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.
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.
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.
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.
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).
(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.
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.
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.
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.
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.
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.
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.
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.
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!