Writing Software: Why Learn FP in JavaScript?

This article is part of the Writing Software series about functional programming and various techniques for writing JavaScript ES6 + programs, starting with the basics. Previous Section: Writing Software: An Introduction





Forget everything you knew about JavaScript and try to interpret this article as if you were a beginner programmer. To help you, we'll cover JavaScipt from the very basics, as if you've never seen JavaScript. Well, if you are a beginner, then you are in luck. Finally, let's try learning ES6 and functional programming from scratch! Fortunately, all new concepts will be explored along the way - but don't count too much on that.





If you are a seasoned developer already familiar with JavaScript or some purely functional language, then you might think JavaScript is a fun way to discover the * [FP] world: functional programming. Put these thoughts aside and try to look at the text with an open eye. You may discover a hidden level in JavaScript programming, a level you never knew existed.





Since this article has in the title "Writing Software", and FP is obviously a way to write a program (using functional composition, higher-order functions, etc.), then you might ask why we don't take some Haskell, ClojureScript, or Elm instead of JavaScript.





JavaScript contains important features necessary for FP:





  1. First class functions. This is the ability to use functions as data, i.e. pass functions as input parameters, return functions, and assign functions to variables and object properties. This property makes it possible for higher-order functions to exist, which in turn makes partial application, currying, and composition possible.





  2. Anonymous functions and lambda syntax. For example, a view notation  x => x * 2



     is a valid JavaScript expression. This syntax makes it much easier to work with higher-order functions.





  3. . - . . , , , , . , . - , . ,  add(1)(2)



      1



      ,  add(1)



    . :





/*
 *   :
 * const add = function (x) {
 *     return function (y) {
 *         return x + y;
 *     }    
 * }
 */
const add = x => y => x + y;
const summ = add(1)(2);
      
      



JavaScript

JavaScript - , , .. . : () (, ), , ; - , - - . - , - , , .





- . :





const foo = {
  bar: 'baz'
};
foo.bar = 'qux'; // 
      
      



, . .





, JavaScript:





  1. . "" . .





  2. (). . , , . , ( , ) , .. , .





  3. . - . .  for



    while



      do ... while



    .





: JavaScript "" (.. - . .). , . , JavaScript "" .





: . JavaScript , , , ,  Immutable.js  Mori. , ECMAScript .





, , ,  const



  ES6. ,  const



, . ,  const



  .





 const



  , , . JavaScript  freeze()



, , , - , . , JavaScript , .





: , JavaScript , " ". ( - . .).





. JavaScript, , ES6. , , Babel ( JavaScript , ES6 ES5), , .





: , .





JavaScript ,

, JavaScript , . , - (- - , , - . .). , -. Haskell -, , , - .





, , , , -, , "".





" - , ?" ~ , , . ", "





, , . - , :





" , , "", , , "", "". ~ . " ".





, - , . - -, . , , . , , .





JavaScript , , , .





, :





"... , ++ ( ) Java, "", "", , HTML."





Netscape , , , Scheme ( Lisp). :





" Netscape " Scheme" ".





JavaScript :





" , Java. Perl, Python Tcl Scheme."





, :





  1. Scheme





  2. Java





:





" , , scheme- self- ( ) (, , Self JavaScript - . .). Java, y2k, , ."





"" Java- , - JavaScript:





  • -  new



    , -





  •  class



       extends



     





  • , .





: .





, JavaScript , "" ( Java, Flash ActiveX ).





, , JavaScript.





, , - JavaScript. , WebAssembly - , WebAssembly - JavaScript (AST). , WebAssembly JavaScript, ASM.js.





JavaScript :





, , JavaScript .





JavaScript . JavaScript , , , .





, - , . , (, ) - . , , , . , , , .





JavaScript , ,  npm



.





JavaScript - . , , , , , - , Java, Lisp C. JavaScript , , .





, JavaScript . , , , , ES6 - JavaScript , . JavaScript , , ?





JavaScript , , . Netflix ( Anglular 2+) , RxJS. Facebook , , Facebook Instagram. PayPal, KhanAcademy Flipkart Redux .





: Angular, React, Redux Lodash JavaScript, , Lodash Redux, , JavaScript .





" JavaScript?". , JavaScript , . JavaScript , " " Lisp, . , Haskell , Haskell.





. Haskell , JavaScript , .





, , JavaScript .








All Articles