Is Javascript a Functional Programming Language?

Viewed 56613

Just because functions are first class objects, there are closures, and higher order functions, does Javascript deserve to be called a Functional Programming language? The main thing I think it lacks is Pure Functions, and it doesn't 'feel' like other functional languages, like lisp (although thats not really a good reason for it not to be a functional langauge...)

14 Answers

As we know the functional programming language doesn't allow to change or mutate the elements(state)of functions but in javascript it is allowed in that sense it is not a functional programming language, although it does treat function as first class citizens.

First, we have to define functional programming. I define it as any language that natively supports and privileges the style of programming shared by canonical (or at least widely agreed-upon) functional languages like Scheme, Racket, Haskell or Clojure.

Other languages, like OCaml, Elixir and Scala have much deeper functional support than JS, but still tend to be considered multi-paradigm. Functionalness is a spectrum.

All of this is very much open to endless debate and nitpicking, but this definition seems solid enough to make the case that JS isn't a serious functional language and probably never will be one any more than any other modern, multi-paradigm language with first-class functions.

Let's pick a specific feature. The language should perform tail call optimization so you can natively write recursive functions on linear data structures. Almost all major implementations of JS fail to offer this fudamental feature that we'd expect from a typical "functional" language (by the above definition) and have no plans to at the time of writing (see Are functions in JavaScript tail-call optimized? for details).

Let's give the benefit of the doubt and toss in TCO and still, JS fails to offer even a slight hint of the immutability design goal you'd expect of a "true" functional language. Just getting const in the language took decades, and all objects are mutable by default.

These problems can't really be completely resolved due to backward compatibility; it's not really possible to turn an established multi-paradigm language into a truly functional language after the fact.

JS is about as functional as, say, Python, Perl, PHP or Ruby which all offer map/filter/reduce operations on lists and support first-class functions or procedures. The existence of first-class functions offers enough of an opening to write code that is in a functional programming style. Toss in trampolines and ramda.js and it might seem convincing at a glance.

The question is whether first-class functions are sufficient to make the language "functional". In fact, Wikipedia lists all of the aforementioned as functional languages, but then, that list includes just about every popular, modern, general-purpose language other than C and Go (including at least one that explicitly identifies as not functional by design) so I don't see that this definition offers much distinguishing value.

Related