- Is Javascript a functional language? I know it has objects & you can do OOP with it also, but is it also a functional language, can it be used in that way?
- You know how OOP became/seems like the next evolution in programming, does that mean that 'Functional Programming' is the next evolution(Note: this is NOT a prompt for opinion BUT a prompt for a factual evidence based answer, & this note is more for the moderators than the contributors 😉 ).
- I learn best through examples, maybe someone could show performing the same task in a OOP way & then in a Functional Programming way for myself to understand & compare what functional programming does/is.
I don't really completely understand 'Functional Programming' to be honest 😛 So comparing Javascript to functional programming may be totally incorrect.
To put Functional programming in laymans terms: is it simply the benefit of abstration THROUGH using anonymous functions?
Or is that way too simple? In a simple way, OOP is the benefit of abstraction through objects, but I believe thats being a little too simplistic to describe OOP.
Is this a good example of functional programming?…
Javascript OOP Example:
// sum some numbers
function Number( v )
{
this.val = v;
}
Number.prototype.add( /*Number*/ n2 )
{
this.val += n2.val;
}
Functional programming example:
function forEach(array, action)
{
for (var i = 0; i < array.length; i++)
action(array[i]);
}
function add(array)
{
var i=0;
forEach(array, function(n)
{
i += n;
});
return i;
}
var res = add([1,9]);
Best Answer
Sometimes, people will say functional programming, when what they mean is imperative programming or procedural programming. Strictly speaking, functional programming is:
Although Javascript is not widely known or used as a functional language, it does have some functional elements:
Scheme is a dialect of Lisp, and probably one of the languages most programmers think of when they discuss functional programming. When it comes to object orientation, Javascript is an object oriented language. But its object orientation is prototype based:
So although Javascript is object oriented, it doesn't follow the more common class based model, as do languages as C++, C#, Java and PHP (and quite a few others). And of course it's also an imperative language, which leads to the confusion with functional programming I described above.
Object orientation and functional programming are just two of the many different programming paradigms, they are different styles of programming with different concepts and abstractions. The key word is "different". There isn't a single paradigm that's better than others or more evolved than others, each and every one fits some scenarios better than the others. Some may be quite older in origin than others, but in evolutionary terms that makes them better, as they have survided longer. But that's not a very smart way of looking at it.
Javascript, as I described above and as quite a few other languages, is multi-paradigm. It allows you to write code in imperative, prototype based object oriented and functional style. It's up to you to choose which one best fits whatever you are building. There are also several single paradigm languages, the canonical example being Java, which only allows for class based object oriented programming1.
You should really resist any urge to treat languages & paradigms as fashion statements. There's an abudance of crap out there, mostly written by fanboys / fangirls or marketing people, with little (if any) knowledge and understanding of programming. Terms like "better", "more evolved" etc, simply don't apply.
That would be a terrible way to learn. Functional and object orientation are quite different styles, and any example other than terribly simple ones would not fit one or the other style.
1 But lately tries to expand its scope to generic programming, let's see how that goes.
In conclusion: