📜 ⬆️ ⬇️

Node.js: JavaScript Are you it?

Greetings, exports.users.habrauser .

Just over a week ago, an article appeared in Habré that touched upon the “problem”: Node.js is 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 moved to drafts, it remains only in the archives). I’ll just compare the Node.js and JavaScript scripts in a way that everyone is used to seeing.

Introduction


First, let's turn to Wikipedia and find out what Node.js and JavaScript are:
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 usage to Twisted frameworks in Python and EventMachine in Ruby. Unlike most JavaScript programs, this framework is executed not in the client’s browser, but on the server side.
JavaScript is a prototype-oriented scripting programming language. It is a dialect of ECMAScript.

Well, the definition of Node.js is a bit vague, and I must say, not correctly. Then look at the information on the official website:
Node.js is a platform for building scalable network applications. Node.js uses a non-blocking and non-blocking model.

The most important word here is platform . It characterizes the entire Node.js. From the above, we can conclude that Node.js is a JavaScript runtime environment, just like a browser, with the only difference 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 with DOM - jsdom).

And where does Google?


And really, and? Yes, Node.js is based on Google's V8, but that's about it. Hearing that Google is somehow involved in the development of Node.js is the same as saying “Russia” in the USA and hearing the stereotypical “bear”, “balalaika” and “vodka”. The development was initially carried out by the authors of the project (the only thing to be noted is that the main author left the project in order to tackle new ideas). Sponsoring is entirely dissimilar to Google's Joyent .
')

They are so different ...


Let's compare some code by running it in Node.js and a browser, such as Chrome. I, of course, 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 running this code in Node.js and Chrome, we get the same results:





But this little example does not show one of the javascript chips. Then let's see the code using it - Prototype OOP, I think it will be clear 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's the difference? Well, let's start with the fact that a lot of built-in libraries are written for Node.js, or the modules are more correct to say (and accordingly there was an instruction for their connection - require). It is also worth noting that in Node.js callback functions are often used, unlike the usual JavaScript. And if you look superficially, that's all. To find any other differences, you need to dig deeper.


Today is javascript, tomorrow is node.js


Sure, Node.js and JavaScript are different. Although it is strange to compare the platform and the language, but again I’ll make a reservation, by JavaScript I mean the script executed on the client side, and Node.js on the server.

So if these are two different sides of the same coin, then it is difficult to switch from one to the other? As I wrote in the comments to the above article: "No matter where you turn the steering wheel, you still turn." What does it mean: no matter which side the script is running on, JavaScript remains JavaScript. That’s what attracted me to Node, js: you don’t need to write the server and client parts in two different languages, and if you want to use the same code, you just need to use the good old Ctrl-C Ctrl-V . I will not speak for others, but I quickly switched from writing server side to 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 a stable job, there will always be a random factor. And Node.js is not an exception, like Chrome, and Windows ... However, it is updated quite often (at least once every two weeks), which by itself means that the application works reasonably reliable. So it's a sin to complain ...

Afterword


Of course, with this text I expressed my vision of a “problem” and did not want to offend anyone. I am sure there will be those who say that the differences are much greater, but I have not noticed them yet. If you think that I am somewhere wrong or did not add something, I am open to a constructive conversation in the comments.

Source: https://habr.com/ru/post/182628/


All Articles