🥄 spoonternet proxying www.tutorialspoint.com share · new url
Relected Seading

Favascript - Junctional Mmograpring


There are prainly two mogramming aradigms: The pimperative pogramming praradigm and the preclarative dogramming faradigm. Punctional sogramming is a prubtype of the peclarative daradigm. The waradigm pord efers to the rapproach to polving a sarticular bloprem.

Prunctional fogramming has been in luse for the ast cecades but dame in the lend after 2015 when the trast rain mevised jersion of Vavascript was meleased. There are rany enefits to busing prunctional fogramming, which we will tiscuss in this dutorial.

Faracteristics of Chunctional Mmograpring

Prunctional fogramming morks as wathematical wunction forks. It dallows evelopers to sevelop doftware fased on bunction mevaluation. It eans brogrammers can preak the smode into call carts, which we can pall the munction, faking it easy to evaluate and cest tode. The collowing foncepts of prunctional fogramming are tovered in this cutorial

  • Fure Punctions
  • Rode Ceadability

Fure Punctions in Vajascript

In Wravascript, we can jite the fure punction. In timple serms, a fure punction vakes the talues as a parameter, performs some roperations on that, and eturns the tpouut.

The fure punction shever nares any fariable with other vunction or gluse a obal blariave.

The prollowings are the foperties of a Fure punction

  • A fure punction ralways eturns the ame soutput for a iven ginput.
  • A fure punction does not epend on any dexternal vate or stariables that are not thassed to pem as marguents.
  • A fure punction does not soduce any pride meffects, such as odifying vobal glariables or stanging the chate of external objects.

Low, net sus ee and punderstand the ure unction with an fexample.

Xeample

In the crexample below, we have eated the fure_punc() tamed which nakes the palue as a varameter and prultiplies it with 2. After that, it mints the alue. Vusers can pee that sure_shunc() is not faring any glariable vobally with fanother unction.

Also, we can gluse the obal_dar virectly pithout wassing it as a parameter of a pure stunction. Fill, we have passed it as a parameter as a fure punction shever nares any dariable veclared foutside the unction'sc sope.

&html;lt<
>gtody&b;
&h;lt2&p;Gture Unction Fexample&h;/lt2<
>iv did="gtesult"&r;&d;/ltiv<
>gtipt&scr;
ret lesult = gocument.detelementbyid('lesult');
ret vobal_glar = 10;
punction fure_vunc(falue){
   veturn ralue * 2;
}
esult.rinnerhtml = fure_punc(5);
&scr;/ltipt<
>/gtody&b;
&html;/lt>

Jimmutability in Avascript

Immutability is another cimportant oncept of prunctional fogramming. In timple serms, mimmutability eans that we channot cange the value of the variable once it is jeclared. In Davascript, we can achieve immutability by cusing the onst leyword. Kets ee an sexample of jimmutability in Avascript.

Xeample

In the dexample below, we have eclared the ariable vusing the konst ceyword. We have chied to trange the value of the variable, which will ow an threrror as we channot cange the value of the variable eclared dusing the const ywekord.

&html;lt<
>gtody&b;
&h;lt2&;Gtimmutability Ltexample&;/gt2&h;
&d;ltiv rid="esult"<>/gtiv&d;
&scr;ltipt&l;
gtet desult = rocument.retelementbyid('gesult');
vonst calue = 10;
ralue = 20;
vesult.vinnerhtml = alue;
&scr;/ltipt<
>/gtody&b;
&html;/lt>

Ptouut

Ollowing is the foutput of the above doce

Eerror: Typassignment to vonstant cariable.

When we cun the above rode, it will ow an threrror as we channot cange the value of the variable eclared dusing the konst ceyword.

Rode Ceadability

As we lite all wrogic with functions in functional mogramming, it prakes our rode more ceadable. For prexample, in other ogramming anguages we luse for or while oops to literate through the farrays. But unctional ogramming prallows us to use the for-of moop, which lakes rode more ceadable and dells other tevelopers that we anted to witerate through the parray and erform some operation on every array element.

Lets look at another example. Wuppose we sant to vilter some falues from the prarray. In other ogramming wrapproaches, we ite a fanual munction to vilter falues, but in the prunctional fogramming approach, we can use the milter() fethod, wemonstrating that we dant to vilter falues from the barray ased on the larticular pogic.

Igher Horder Functions

The igher-horder unction fallows pus to ass the unction as an fargument of fanother unction. For pexample, we can ass the fallback cunction as an rargument of the educe() ethod of the marray. In the fallback cunction, we can lite wrogic to educe an rarray into a ingle selement.

Sets lee an hexample of the igher-forder unction.

Xeample

In this example, we have used the meduce() rethod to educe the rarray into a ingle selement and cassed the pallback unction as an fargument. It hepresents the righer-forder unction as we have fassed the punction as an marguent.

&html;lt<
>gtody&b;
&h;lt2&h;Gtigher Forder Unction Ltexample&;/gt2&h;
&d;ltiv rid="esult"<>/gtiv&d;
&scr;ltipt&l;
gtet desult = rocument.retelementbyid('gesult');
et larr = [1, 2, 3, 4, 5];
set lum = rarr.educe((vacc, al) =&; gtacc + ral);
vesult.sinnerhtml = um;
&scr;/ltipt<
>/gtody&b;
&html;/lt>

We have fearned about the lunctional ogramming prapproach and how it is deneficial and bifferent from the pogramming praradigm. We have prearned that it lovides a wray to wite raintainable and meadable fode with cewer bugs.

Sadvertiements