Is Javascript a Functional Programming Language?
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:
- Has first-class functions
- Javascript is this!
- 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.
hvgotcodes
Software developer consultant working in the Northern VA area...
Updated on September 15, 2021Comments
-
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 over 13 yearsJavaScript is object oriented. OO does not require classes, it does however require objects.
-
Ashley Grenon over 13 yearsyeah, I agree it's a mix and several different things.
-
Eric Mickelsen over 13 yearsBy that reasoning, F# can no long be called functional.
-
shuhalo over 13 yearsIs there a source which uses "functional programming" to refer to languages with functions being first-order citizens?.
-
josesuero over 13 yearsbut 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 over 13 yearsdesign 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 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 over 13 yearsIt 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 over 13 yearsRight. 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 over 13 yearsLater 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 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 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 over 11 yearsHm, I think Javascript holds reference to variable but doesn't hold reference to value.
-
johnbakers over 10 yearsJavaScript has the notion of partials, and partially applying parameters, so I wonder if your statement that it does not support this is incorrect?
-
johnbakers over 10 yearsThe 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 over 10 yearsYou 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 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 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 over 10 yearsJavaScript supports immutability afaik.
-
missingfaktor over 10 years@fka, can you expand on what you mean?
-
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 over 10 years... but the latest JS would support it.
-
rich remer about 10 yearsUnderstanding variable scoping is critical to effective programming in any language. Javascript is not alone in this.
-
Kris almost 10 yearsJavascript is not object oriented, it is prototype based.
-
Andrew S over 6 yearsJavaScript is programming soup. A bit of this and a bit of that.
-
ggorlen over 2 yearsHow does this answer the question?
-
theprogrammer over 2 yearsSo 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?