Node.js: are you JavaScript?

    Greetings, exports.users.habrauser .

    A little over a week ago, an article appeared on the hub that addressed the "problem": Node.js - is it JavaScript or not. Some of the arguments presented in the article were fair, but, alas, unfounded. Other arguments were completely absurd and not true. I will not write about the knowledge of the author of the article in this area, I will not even give links to this article (so that the article is transferred to drafts, it remains only in the archives). I’ll just compare Node.js and JavaScript scripts in a way that everyone is used to seeing.

    Introduction


    To get started, turn to Wikipedia and find out what there is Node.js and JavaScript:
    Node or Node.js is a server-side implementation of the JavaScript programming language, based on the V8 engine. Designed to create scalable distributed network applications such as a web server. Node.js is similar in use to Python's Twisted frameworks and Ruby's EventMachine frameworks. Unlike most JavaScript programs, this framework is not executed in the client’s browser, but on the server side.
    JavaScript is a prototype-oriented scripted programming language. It is a dialect of ECMAScript.

    Well, the definition of Node.js is a bit vague, and I must say it is not correct. Then look at the information on the official website:
    Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I / O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

    The most important word here is the platform . It characterizes the whole Node.js. From all of the above, we can conclude that Node.js is a JavaScript runtime, just like a browser, with the only difference being that we do not have access to the DOM (and actually, why is it needed on the server side ?; however, there is a library for working with DOM - jsdom).

    And where does Google?


    And really, moreover? Yes, Node.js is based on Google’s V8, but that’s it. To hear that Google is somehow involved in the development of Node.js is the same as saying “Russia” in the USA and hearing the stereotyped “bear”, “balalaika” and “vodka”. The development was initially carried out by the authors of the project (the only thing to note is that the main author left the project to deal with new ideas). Sponsorship is led by a completely different company from Google Joyent .

    They are so different ...


    Let's compare some code by running it in Node.js and a browser like Chrome. Of course, I will not check the trivial console.log ("Hello, World!"); I’ll take something more complicated. For example, the function of calculating factorial using tail recursion:

    var factorial = function (n, a) {
      return n < 2 ? a : factorial(n - 1, a * n);
    };
    console.log(factorial(10, 1));
    

    By executing this code in Node.js and Chrome we get the same results:





    But this small example does not show one of the JavaScript chips. Then let's see the code using it - Prototype OOP, I think it will be understandable without further explanation:

    var Celsius2Fahrenheit = function (val, unit) {
      this.val = val;
      this.unit = unit;
    };
    Celsius2Fahrenheit.prototype.setVal = function (degrees) {
      var degreesArray = degrees.split(" ");
      this.val = degreesArray[0];
      this.unit = degreesArray[1];
    };
    Celsius2Fahrenheit.prototype.convert = function (to) {
      if (this.unit != to) {
        this.unit = to;
        switch (to) {
          case "C": {
            this.val = Math.round((this.val - 32) * 5 / 9);
          } break;
          case "F": {
            this.val = Math.round(this.val * 9 / 5 + 32);
          } break;
        }
      }
    };
    var c2f = new Celsius2Fahrenheit(30, "C");
    console.log(c2f.val + c2f.unit);
    c2f.convert("F");
    console.log(c2f.val + c2f.unit);
    c2f.setVal("100 F");
    console.log(c2f.val + c2f.unit);
    c2f.convert("C");
    console.log(c2f.val + c2f.unit);
    

    The code may not be completely "elegant", but the result is very similar to the correct one. Check for yourself, in the browser and in Node.js it is the same:
    30C
    86F
    100F
    38C


    So what is the difference? Well, let's start with the fact that many built-in libraries have been written for Node.js, or it is more correct to say modules (and, accordingly, there was an instruction for connecting them - require). It is also worth noting that Node.js often use callback functions, unlike the usual JavaScript. And if you look superficially, that's all. To find any other differences, you need to dig deeper.


    JavaScript Today, Tomorrow Node.js


    Sure, Node.js and JavaScript are different. Although comparing the platform and the language is somehow strange, but again I will make a reservation, by JavaScript I mean a script executed on the client side, and Node.js on the server.

    So if these are two different sides of the same coin, is it difficult to switch from one to the other? As I wrote in the comments on the aforementioned article: “It doesn’t matter where you turn the steering wheel, you still turn.” Which means: no matter which side the script is running on, JavaScript remains JavaScript. This was what attracted me Node, js: you do not have to write the server and client parts in two different languages, and if you need to use the same code, then you just need to use the good old Ctrl-C Ctrl-V. I will not speak for others, but I quickly switched from writing the server side in PHP to Node.js, and did not notice any difficulties.

    We want stability!


    About the stability of applications, you can talk forever. No one can guarantee you stable operation, there will always be a random factor. And Node.js is no exception, like Chrome and Windows ... However, it is updated quite often (at least once every two weeks), which by itself means quite reliable operation of the application. So it’s a sin to complain about it ...

    Afterword


    Of course, with this text I expressed my vision of the “problem” and did not want to offend anyone. I am sure there will be those who say that there are many more differences, but I have not noticed them yet. If you think that I’m wrong somewhere or didn’t finish something, I’m open for constructive discussion in the comments.

    Also popular now: