Javascript Functional programming basics | Function Object tutorial
- Admin
- Dec 31, 2023
- Javascript
Learn different programming paradigms related to functional programming. How does implement in javascript?
JavascriptFunctional Programming
It is one programming type to handle computations and mathematical applications. Haskell, Clojure, Python, Erlang, and Scala are full-featured functional programming languages. Javascript is not a strictly functional programming language. It supports some of the techniques in it.
Javascript Applicative Programming
It is one more pattern for a function as a parameter and executes each element in an array or collection. Javascript introduced First-class objects/citizens to implements.
Javascript already implemented this paradigm with Array and String methods like forEach method, filter, every(), and Map() methods.
these methods are iterated on each element and apply the function to each element.
We will see examples in the sections Functions are also objects in javascript Functions also contain properties
let us see simple function creation
console.log(testmethod); // returns function code
console.log(testmethod.name); // returns testmethod
function testmethod() {
console.log("Howdie");
}
After running the above code, It just returns a function. Functions also have properties, one of the properties is the name. testmethod.name prints the function name.
First Class Functions
What is First Class Functions?
First-class functions in any programming are called for functions if Functions can be treated the same as variables. whatever you do with variables, you can do everything with javascript functions also.
that means functions are saved to variables, passed as a parameter to the function, and returned from the function.
It is also called First class citizens or First class objects of Javascript
We will see detailed tutorials using functions equal to using a variable with examples below.
- Functions can be assigned to variables/constants/objects/arrays
- Functions can be passed as a parameter to a function
- Functions returns from another Function
Function saved to variables/constants/objects/arrays
Here we assigned the anonyms function to the variable. And calls the function with that variable. Named functions are also called this. Functions and variables/values are treated the same in javascript Function assign to a variable The function also can be assigned to variables.
let functionVariable = function method() {};
Assign to a constant The function also can be assigned to constants.
let functionVariable = function method() {};
const function1 = function () {
console.log("function printed");
};
function1();
Function assign to Object It is an example of assigning or storing a function as a variable in an Object.
let obj = { method: function () {} };
Function save to array This is a syntax for storing functions like an element in arrays.
myarray.push(function method() {});
Passing as a Function argument to other functions
In this example, passing a function as an argument to a function Here function2 is passed as an argument to display function function2 as a callback function.
function function2() {
return "function2 ";
}
function display(helloMessage, name) {
console.log(function2() + name);
}
display(function2);
Function returns function like a variable value
function mainfunction() {
return function () {
return "return function as value";
};
}
From the examples of the above functions, We can say javascript in functions is treated as a variable. we can easily say, first-class citizens.
High Order Functions are functions that take a function as an argument and return the function as parameters.
Data is immutable in Functional programming
In functional programming, the state or data is immutable meaning not able to update it.
For a variable,
If you want to change data in a variable, you need to create a new variable. To achieve this use the const keyword for a declaration.
For arrays:
you need to create a new array and concat the existing array You can use Object.assign() method or spread operator to achieve this.
You can check here for more information.
For objects:
Data in the object does never change, So you need to use Object.assign for cloning the object and updating it.
In functional programming data is never changed, You need to create a copy of data to change it.
functions provide a copy of data and javascript native map and reduce methods are supported for returning copy/creation of existing data.