My Relationship with JavaScript My journey with JavaScript started way back in 1997 with Netscape Navigator 3. Back then you couldn’t do much with it. . At the time, that was some high tech 💩. Being able to change content on the page by just hovering. mind blown. This was all pre-DHTML BTW, so you couldn’t even hide / show DOM elements. It was still oh so magical. The coolest use of JavaScript was to create mouseovers At this point in time, JavsScript’s evolution was very slow, being used primarily for form validation. There wasn’t much progress or interest in JavaScript, nothing like today. It was an extra, an addon, a nice-to-have, but it definitely wasn’t the main attraction and you would still have to be sure your application could run with JavaScript disabled. Then frameworks entered the market in a big way. jQuery, Knockout, Angular, React, Vue, etc. They came slow at first. Now we get a new framework daily. We are also seeing . It took us forever to get ES6. People have already stopped talking about ES7 and are onto ES8! the evolution of JavaScript itself accelerating We also have such as TypeScript, CoffeeScript, ClojureScript, ELM, etc. an infinite number of alternatives It can be overwhelming and it’s to keep up with everything. impossible The Wrong Path When JavaScript first started to mature, Object Oriented Programming (OOP) started to sneak its way into the language… . and I loved it I started playing with JavaScript and all the different ways I could create a class. I could do proper inheritance. I started to think to myself, JavaScript is starting to become a language. finally real It wasn’t until many years later that I would discover OOP in JavaScript was a terrible mistake. and at first things seemed very promising, but the complexity was mind-bending. I tried to force what I knew from C# into JavaScript Because JavaScript’s prototypal inheritance does not work like C#, I would find myself writing on a daily basis. If you didn’t do everything exactly right, it was a nightmare. Private methods and values had to be prefixed with underscores or even worse wrapped in closures. console.log(this) Who am I now? Not only did OO JavaScript come with all the problems of OOP, but it added many new problems on top of it. Then I Discovered Functional Programming . I would see the code and understand the code, but I didn’t get . Eventually, . I took an on EDX for with the intent of applying those techniques to JavaScript. I didn’t get it at first why I forced myself to learn it Introduction to Functional Programming course FREE Functional Programming gave me a perspective, it made me see programming in a much different way. new It was weird and took me a while to get used to it. Everything was backwards. Immutable. Foreign. at first Slowly, I would try to solve code the functional way. Because I was unfamiliar, it took longer and was more of an academic exercise, but and get better at it. Then I got . I started to get it why . Slowly common language features started to disappear from my code. My code looked like a completely different language. Was this still JavaScript I was writing? My code became simplified and reusable No more ‘var’ All my 's were replaced with . When my code became and my functions became , disappeared completely. var const immutable pure var I remember looking at my code and marveling at not seeing a single or even , everything became . My interest levels started rising. var let const The ‘for’ Loop The loop was one of the first things to go. I started replacing the majority of my loops with , and . The loops that required something extra were replaced with recursion or libraries like . Before you start to mutter the word , . for for filter map reduce lazy.js break read this _JavaScript’s for loop has served us well, but it is now obsolete and should be retired in favor of newer functional…_hackernoon.com Rethinking JavaScript: Death of the For Loop The loop is now completely extinct in my codebase. If you do happen to stumble across one, point it out so I can kill it. for The ‘if’ Statement The statement was soon the next thing to go. I stopped writing large blocks of code nested inside an if-else. (this is also good practice to do with OOP). The logic was extracted into functions. After this, it became obvious to convert the to a simple operator. if if ternary _Thinking functionally has opened my mind about programming. It has given me a greater depth of understanding of code…_hackernoon.com Rethinking JavaScript: The if statement It is now almost impossible to find an statement in my code. I will use one for readability for other programmers. if rarely RIP ‘switch’ With the and gone, the next feature I eliminated was the . These weren’t used often, but I wanted a functional alternative. if for switch _In my last 3 articles I convinced you to remove the if statement, kill the for loop, and never use break. Now I’m going…_hackernoon.com Rethinking JavaScript: Eliminate the switch statement for better code I also really like Ramda’s as a replacement for switch. cond operator No More ‘this’ That’s right, you heard me correctly! has disappeared. If you haven’t been with me up until this point, . this completely you should be with me now Functional JavaScript will allow you to write entire applications free of ' . this’ Now, there is only data and functions and we have absolutely no need for . I started to see an object as mutable state + functions. I didn’t have a need for mutable state or for functions to be attached to objects, so I disconnected them. this I even wrote an article talking about some of the benefits of decoupling methods from their objects. _One thing I always end up doing in my project is decoupling methods from their objects. map, filter and reduce are not…_hackernoon.com Functional JavaScript: Decoupling methods from their objects Object Oriented Design is Unnecessary Looking back, I now realize OOP added a complexity that was not necessary. I found I could perform the same tasks without having a mutable state. The code felt lighter because I was no longer passing around these heavy objects. There was just data and functions. These functions now became more reusable now that they weren’t tied to an object. I no longer had to worry about all the issues that come along with classical inheritance, which JavaScript does poorly. JavaScript’s lack of access modifiers like private, public, internal or protected was no longer an issue. Access modifiers were created to solve issues that OOP created. Issues that no longer exist in functional JavaScript. Summary My code now looks completely different. It consists of a collection of many , organized into . I use these functions to compose into more complex functions. A large majority of functions are one-line . pure functions ES6 modules lambda expressions that immediately return a value I now see software inputs as data streams and program reactively on those streams. My understanding of functional programming has given me more options at my disposal to me to solve common problems. I also learned that and can be used as much or as little as you need in existing projects . C#’s LINQ is a great example of functional design in an OO language. functional programming is inclusive today Functional Programming can be beautiful. _Story Time_hackernoon.com The beauty in Partial Application, Currying, and Function Composition. I will respond to every question and comment, so don’t be shy! I know it’s a small thing, but it makes my day when I get those follow notifications on Medium and Twitter ( ). Or if you think I’m full of shit, tell me in the comments below. @joelnet Cheers! _Read the latest stories written by Joel Thoms on Medium. Computer Scientist and Technology Evangelist with 20+ years of…_medium.com Latest stories written by Joel Thoms - Medium