Node.js - Why I love working in Node - Part 1

Here is a story how I fall in love with Node.js and why I love working with it.

If you don’t know or new to Node.js let me tell you Node.js is not a programming language. It’s a runtime environemnt for running javascript outside browser. If you never used javascript or learned about javascript via Node only then let me tell you that javascript was a only meant to run on browser. It was only way you can run a code in browser. I am telling this because there are new technologies like WebAssembly that also runs on browser but that the topic for different article. But now javascript is everywhere in server-side backend, IoT, Machine Learning etc also accompanied by easy learning curve being a dynamic language and Node.js is fast and evolving fast.

Let us first of all look about Javascript and then move to Node.js.

Javascript

Javascript is a dynamic language and only way you can run your code in browser. It is mainly used to manipulate DOM (Data Object Model) in browser. DOM in simple terms a tree like representation of HTML elements. Also javascript it used to connect to servers to bring data to frontend using AJAX but now new fetch() API can be used which uses promises.

If you know languages like C,C++,Java or Golang those are statically typed language where you have declared the data type of variable you want to declare and if you know languages like Python or Ruby those are dynamic where you don’t have to declare type of variable interpreter determines it automatically.

Javascript designed by Brendan Eich was released in year 1995 under name Mocha with Netscape Navigator and then later changed to Livescript in official release and then again changed name to Javascript when Netscape Navigator 2.0 was released. When I heard about Javascript I had a wrong knowledge that syntax would be like Java but later came to know name was intentionally kept as Javascript because at that time Java was the hot new language.

Now it’s 2018 and there is a international body named ECMA International that releases specifications for javascript named under ECAMScript for independent implementation. There latest release it ES9 or ECMAScript 9 which introduces spread parameters and some other features Javascript. ES6 is the version which introduced most of the featurs in Javascript like classes, modules, generators, typed arrays etc and the version with which I am most acquainted using. One the main feature of javascript is using Babel with which we can use new introduced features even if it not introduced by browser vendors in there Javascript engines.

Javascript engine is used to run javascript source code inside browsers. It has it parsers, compilers, optimizers etc. Every browser vendor has it’s own implementation of Javascript engine - Google Chrome uses V8, Mozilla Firefox uses SpiderMonkey, Microsoft Edge uses ChakraCore and Apple Safari uses JavascriptCore. Even though implementation is somewhat different in every engine but basic working is same and it follows as: parse, compile, optimize, execute. I am not going in detail in each step, if I go it will take it’s own article to explain and how different engines implement it.

Now with improvement in engines a javscript is the main driving force using Node.js for developer toolchain in frontend as industry moves towards Single Page Application (SPA) and Progressive Web App (PWA). In simple way to explain SPA, traditionally in web page when we click a link it takes us to a new HTML page in SPA there is a single HTML file the entry point and everything is managed by javascript where only element where all the contents will be displayed is there in HTML with basic HTML tags like head, title, meta etc which sometimes create problems for search engine crawlers. PWA is a emerging technology where a web app can be used as native mobile app which will be still running in web view with removed url bar and request is managed by a special javascript code called Service Worker which stores required data to display the page when network is not availible. These are simpler explanation of concepts in future artiles I will discuss them in more detail.

Examples of Javascript

Printing a line

console.log("Hello World")
Output
Hello World

Adding two numbers

var x = 1
var y = 2
console.log(x+y)
Output
3
Here you can see there is no type declaration

Functions

function add(x,y) {
  return x+y
}
console.log(add(1,3))
add = function (x,y) {
  return x+y
}
console.log(add(1,3))
Output
4

Functions in ES6

add = (x,y) => {
  return x+y
}
console.log(add(1,3))
add = (x,y) => x+y
console.log(add(1,3))
Output
4

Dynamic nature

var x = 1
console.log(x)
var x = "name"
console.log(x)
Output
1
name

Making Javascript like static languages

As you have seen in last example we can assign any value to any variable which is easy to code but creates problem when building large applications. This is the reason why many languages are there which can transpile to Javscript as it only language which can run on browsers. Languages are there like Typescript, Reason using Bucklescript, Purescript, Kotlin, Dart, Elm, Coffescript and many many others.

Most popular among them is Typescript by Microsoft as superset of Javascript and many other features. Every javacript code is a valid typescript source code. Angular 2 and above uses typescript as default langauage due to it’s rich features.

Functional programming now days becoming popular and javascript is not far behind there is 2 implementation which I like is Reason using Bucklescript where Reason is by Facebook and Bucklescript was by Bloomberg but now is seperate entity which provides OCaml like syntax (more or less) and Purescript which is influced by Haskell as pure funtional languages. As a facebook product Reason is officially supported by React and React Native.

Dart is another programming language by Google which can run on servers using Dart VM, mobile application as Flutter is a Dart framework use to build cross platform apps and also in browser by transpiling to Javascript. In Dart everything is a object, any object is an instance of a class and objects can act as function too.

Kotlin is programming langauge which is developed by IntelliJ a company expert in building IDEs for technologies like IntelliJ IDEA, PyCharm etc. Langauge is develped to interoperable with Java and remove the verbose-ness of Java language and also to be null safe. Originally meant to run on JVM and transpile to Java but now native implementation is on it’s way.

Elm is pure functional language which is great for replacement of frameworks or libraries like React and gives output in HTML, CSS and Javascript.

Post is getting long. Ending it here and in next part we will directly jump to Node.js what is it, how it works and why it is important for me.