Is Javascript a Functional Programming Language?

54,461

Solution 1

Repeating my own answer to a similar question,

There's no accepted definition of functional programming language.

If you define functional language as the language that supports first class functions and lambdas, then yes, JavaScript *is* a functional language.

If you also consider the factors like support for immutability, algebraic data types, pattern matching, partial application etc then no, JavaScript *is not* a functional language.


I'd encourage you to read the following related blog posts (and also the comments below them):

Solution 2

I would say that it is a multi-paradigm language.

EDIT: It's multi-paradigm and includes functional constructs.

Solution 3

if you stretch and twist the term "functional programming" to the point of philosophical discussions, this question may be open again. However, then you end up on the level of useful questions like "Is C++ really a programming language"?

The answer to your question on more daily level is "no".

Functional programming means that the program is conceptualized as a evaluation of a function, rather than a control flow. The code is a description of functions, and has no inherent concept of a control flow.

JavaScript has got a control flow and is conceptualized as a imperative language. From its design objective, it is clearly not a functional language.

Solution 4

The term "functional programming" language is so overloaded these days it's almost useless. There are two dominant meanings:

  1. Has first-class functions
    • Javascript is this!
  2. Is based on functions as used in the lambda calculus, with an emphasis on avoiding persistent mutable state (often replacing it with parameters passed to functions)
    • As commonly written, Javascript is not remotely this!

Pick your meaning and then the question is answerable.

Solution 5

I don't think there a concrete definition of functional programming , however many of things people consider "functional programming" can be done with javascript. Here is a good brief example in this article.

Share:
54,461
hvgotcodes
Author by

hvgotcodes

Software developer consultant working in the Northern VA area...

Updated on September 15, 2021

Comments

  • hvgotcodes
    hvgotcodes over 2 years

    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...)

  • Incognito
    Incognito over 13 years
    JavaScript is object oriented. OO does not require classes, it does however require objects.
  • Ashley Grenon
    Ashley Grenon over 13 years
    yeah, I agree it's a mix and several different things.
  • Eric Mickelsen
    Eric Mickelsen over 13 years
    By that reasoning, F# can no long be called functional.
  • shuhalo
    shuhalo over 13 years
    Is there a source which uses "functional programming" to refer to languages with functions being first-order citizens?.
  • josesuero
    josesuero over 13 years
    but that doesn't answer the question of whether it is also functional. Being multi-paradigm implies supporting multiple paradigms. Is one of these paradigms functional programming?
  • josesuero
    josesuero over 13 years
    design objective? What do you mean? Last I checked, one of its sources of inspiration was Scheme. I'd say it's pretty clear that one of its design objectives was to support functional programming as well as a bucketful of other paradigms
  • Chuck
    Chuck over 13 years
    @user411768: Actually, another answerer linked to Wikipedia's article, which uses that definition. en.wikipedia.org/wiki/Javascript — Joel Spolsky also implied this definition in his "Can your programming language do this?" post on the benefits of "functional programming"
  • shuhalo
    shuhalo over 13 years
    It supports functional programming as much as C++ does, if you write appropriate foundations for this by yourself - as much as you can emulate imperative syntax in, say, Haskell with a little work. Nevertheless the syntax of JavaScript leads to it being thought of a a work flow rather than the evaluation of a function. For that reason, I (or most functional programmers) regard applying the term "functional" as too extensive.
  • Brian Onn
    Brian Onn over 13 years
    Right. According to Wikipedia, F# is exactly what I just called Javascript: "F# [...] is a multi-paradigm programming language [...] that encompasses functional programming as well as imperative object-oriented programming disciplines"
  • jbeard4
    jbeard4 over 13 years
    Later versions of Mozilla's implementation of JavaScript (staring with 1.7) have pattern matching in the form of destructuring assignments: developer.mozilla.org/en/New_in_JavaScript_1.7#section_20
  • josesuero
    josesuero over 13 years
    @user411768: so you're saying that whether or not a language is functional depends on the design of its standard library? I've never heard that definition before. Java has most of the tools needed to program in a functional style (closures and anonymous functions, for example), which C++ (currently) doesn't. I think that makes JS a lot more FP than C++ is. The fact that the language doesn't force you to program in a FP style doesn't make it "less functional", does it?
  • shuhalo
    shuhalo over 13 years
    (i) The standard library is a part of the standard, just as the syntactical features are, and emphasizes a certain idiomatic and conceptual style. E.g. "C++ with STL" is very different from "C with classes". It has an impact. (ii) JavaScript features object orientation, first-class-citizen functions - the features are rather orthogonal to the imperativ/functional-dichotomy. However, neither does it directly implement currying, nor does it provide purity, nor has it ever been intended for this. (iii) My last words on that, see first paragraph of the post.
  • aeracode
    aeracode over 11 years
    Hm, I think Javascript holds reference to variable but doesn't hold reference to value.
  • johnbakers
    johnbakers over 10 years
    JavaScript has the notion of partials, and partially applying parameters, so I wonder if your statement that it does not support this is incorrect?
  • johnbakers
    johnbakers over 10 years
    The statement that JavaScript and C++ offer the same functional programming conveniences is certainly wrong. JavaScript makes functional programming quite straightforward and simple without all of the messy constructs you must go through in C++ to achieve the same thing. There are plenty of great C++ coders who prominently say that functional programming is really not encouraged in C++, however articles on doing functional programming in JavaScript abound plentiful
  • johnbakers
    johnbakers over 10 years
    You note that, as commonly written, JavaScript is not using your second point, but that certainly doesn't mean that there aren't programmers doing exactly that, and that the language doesn't support such a feature, because it certainly does
  • missingfaktor
    missingfaktor over 10 years
    @OpenLearner, partial application is supported by pretty much every language I can think of, even C. For a certain definition of "support" anyway. The case with JS is no different. The point is whether partial application is effortless and first class in that language. In JS, it isn't. If you're curious about what I mean, take a look at OCaml or Haskell.
  • Chuck
    Chuck over 10 years
    @OpenLearner: Well, yeah, but the same is true of Java and lots of other languages that are generally considered to be strictly imperative — you can write them in a functional style, but it's not the language's happy path.
  • fka
    fka over 10 years
    JavaScript supports immutability afaik.
  • missingfaktor
    missingfaktor over 10 years
    @fka, can you expand on what you mean?
  • Erik  Reppen
    Erik Reppen over 10 years
    @missingfaktor Constants. Do-nothing getter/setters. Seal and Freeze methods. But that's all newer ecma5 JS you won't see in browsers regularly until a lot of old IEs have passed away. We've been able to encapsulate instance vars in an object that could only be accessed with a getter method since pre-2000 I would guess. Of course, that's immutability support in the same sense that Java can emulate first class functions with one-method objects.
  • Erik  Reppen
    Erik Reppen over 10 years
    ... but the latest JS would support it.
  • rich remer
    rich remer about 10 years
    Understanding variable scoping is critical to effective programming in any language. Javascript is not alone in this.
  • Kris
    Kris almost 10 years
    Javascript is not object oriented, it is prototype based.
  • Andrew S
    Andrew S over 6 years
    JavaScript is programming soup. A bit of this and a bit of that.
  • ggorlen
    ggorlen over 2 years
    How does this answer the question?
  • theprogrammer
    theprogrammer over 2 years
    So would java be considered functional as well since they added lambda support in java 8 or something? Or is that lambda different from the lambda you are talking about?