Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 756193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:13:01+00:00 2026-05-14T15:13:01+00:00

I’ve been working with node.js for a while on a chat app (I know,

  • 0

I’ve been working with node.js for a while on a chat app (I know, very original, but I figured it’d be a good learning project). Underscore.js provides a lot of functional programming concepts which look interesting, so I’d like to understand how a functional program in JavaScript would be setup.

From my understanding of functional programming (which may be wrong), the whole idea is to avoid side effects, which are basically having a function which updates another variable outside of the function so something like

var external;
function foo() {
   external = 'bar';
}
foo();

would be creating a side effect, correct? So as a general rule, you want to avoid disturbing variables in the global scope.

Ok, so how does that work when you’re dealing with objects and what not? For example, a lot of times, I’ll have a constructor and an init method that initializes the object, like so:

var Foo = function(initVars) {
   this.init(initVars);
}

Foo.prototype.init = function(initVars) {
   this.bar1 = initVars['bar1'];
   this.bar2 = initVars['bar2'];
   //....
}

var myFoo = new Foo({'bar1': '1', 'bar2': '2'});

So my init method is intentionally causing side effects, but what would be a functional way to handle the same sort of situation?

Also, if anyone could point me to either a Python or JavaScript source code of a program that tries to be as functional as possible, that would also be much appreciated. I feel like I’m close to “getting it”, but I’m just not quite there. Mainly I’m interested in how functional programming works with traditional OOP classes concept (or does away with it for something different if that’s the case).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:13:01+00:00Added an answer on May 14, 2026 at 3:13 pm

    You should read this question:

    Javascript as a functional language

    There are lots of useful links, including:

    • Use functional programming techniques to write elegant JavaScript
    • The Little JavaScripter
    • Higher-Order JavaScript
    • Eloquent JavaScript, Chapter 6: Functional Programming

    Now, for my opinion. A lot of people misunderstand JavaScript, possibly because its syntax looks like most other programming languages (where Lisp/Haskell/OCaml look completely different). JavaScript is not object-oriented, it is actually a prototype-based language. It doesn’t have classes or classical inheritance so shouldn’t really be compared to Java or C++.

    JavaScript can be better compared to a Lisp; it has closures and first-class functions. Using them you can create other functional programming techniques, such as partial application (currying).

    Let’s take an example (using sys.puts from node.js):

    var external;
    function foo() {
        external = Math.random() * 1000;
    }
    foo();
    
    sys.puts(external);
    

    To get rid of global side effects, we can wrap it in a closure:

    (function() {
        var external;
        function foo() {
            external = Math.random() * 1000;
        }
        foo();
    
        sys.puts(external);
    })();
    

    Notice that we can’t actually do anything with external or foo outside of the scope. They’re completely wrapped up in their own closure, untouchable.

    Now, to get rid of the external side-effect:

    (function() {
        function foo() {
            return Math.random() * 1000;
        }
    
        sys.puts(foo());
    })();
    

    In the end, the example is not purely-functional because it can’t be. Using a random number reads from the global state (to get a seed) and printing to the console is a side-effect.

    I also want to point out that mixing functional programming with objects is perfectly fine. Take this for example:

    var Square = function(x, y, w, h) {
       this.x = x;
       this.y = y;
       this.w = w;
       this.h = h;
    };
    
    function getArea(square) {
        return square.w * square.h;
    }
    
    function sum(values) {
        var total = 0;
    
        values.forEach(function(value) {
            total += value;
        });
    
        return total;
    }
    
    sys.puts(sum([new Square(0, 0, 10, 10), new Square(5, 2, 30, 50), new Square(100, 40, 20, 19)].map(function(square) {
        return getArea(square);
    })));
    

    As you can see, using objects in a functional language can be just fine. Some Lisps even have things called property lists which can be thought of as objects.

    The real trick to using objects in a functional style is to make sure that you don’t rely on their side effects but instead treat them as immutable. An easy way is whenever you want to change a property, just create a new object with the new details and pass that one along, instead (this is the approach often used in Clojure and Haskell).

    I strongly believe that functional aspects can be very useful in JavaScript but ultimately, you should use whatever makes the code more readable and what works for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This video series on nHibernate is great and they are… May 14, 2026 at 8:30 pm
  • Editorial Team
    Editorial Team added an answer Disclaimer: C++ is my favorite language, so I'm a bit… May 14, 2026 at 8:30 pm
  • Editorial Team
    Editorial Team added an answer Option one - Have Foo call Bar when it's updated… May 14, 2026 at 8:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.