The Basic Primitives Of Nodejs: A Javascript Environment

Webnexs
3 min readAug 11, 2020

Nodejs is ultimately a JavaScript execution environment with some peculiar features within it.

Any JavaScript which executes in the Chrome browser would execute in Nodejs environment too. It is also an environment that works outside the browser to overcome such kind of browser limitations.

Since browser JavaScript has some limitations like not being able to do I/O, interface with databases, etc. But it provides all the necessary features for general-purpose programming.

Basic Primitives Of Nodejs

Nodejs includes the following primitive types:

  • String
  • Number
  • Boolean
  • Undefined
  • Null
  • RegExp
Basic Primitives Of Nodejs

Loose Typing

JavaScript supports loose typing(i.e. you can easily add a string to an integer and get the result as a string) as in browser’s JavaScript. In simple, loose typing means it doesn’t care about the types.

Read: Basics of Nodejs: A complete roadmap to learn

Object Literal

Object literal syntax is the same as the browser’s JavaScript.

Example: Object

var obj = { authorName: ‘Ryan Dahl’ , language: ‘Nodejs’ }

Functions

Functions are primary elements in Node’s JavaScript which are similar to the browser’s JavaScript. Each function has attributes, features, and properties. It can be tended to act like a class in JavaScript.

EXAMPLE: FUNCTION

function display (x) {console.log(x);}display(100);

Read: Nodejs — OS module

Buffer

Buffer is an additional data type that is not available in the browser’s Javascript. Buffer is mainly used while reading from a file or receiving packets over the network to store data.

EXAMPLE

var buf = Buffer.from('abc');console.log(buf);

Process Object

Each Nodejs script runs in a process that includes object to get all the information about the current process of Node application.

To get process information in REPL using process object, use the following sample code;

> process.execPath'C:\\Program Files\\nodejs\\node.exe'> process.pid1652> process.cwd()'C:\\'

Defaults To Local

In the global scope, even Node’s JavaScript slightly differs from the browser’s JavaScript. The key difference between those two Javascript is, In the browser’s JavaScript the variables declared without var keyword become global. Whereas In Nodejs, everything becomes local by default.

Read: Nodejs — Packages and Modules: How to use it

Access Global Scope

In Nodejs, a global object represents the global scope which is primarily a window object in a browser.

In order to add an object in the global scope, you can use export or module.export to add the additional objects. Likewise, an import modules/object using require() function to access it from the global scope.

For example, use exports.name = object to export an object,

Exports.log = {Console: function(msg) {console.log(msg);} ,File: function(msg) {// log to file here}}

Now, you can use require() function anywhere in your Nodejs projects by importing log object.

I hope this overview helped you in a good way, we always ensure our reader’s satisfaction on any topic. To know more about Nodejs, Contact us.

--

--