What are the basic primitives of Nodejs?

By Haribabu | Posted on February 5, 2020

Learn some basic primitives of Nodejs here in this blog to get started with the JS world.

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

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

Get your diverse range of well-optimized NodeJS development services here!!!

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

primitives of Nodejs

I’d list the various primitives of Nodejs that helps in the developmental process:

  • String
  • Number
  • Boolean
  • Undefined
  • Null
  • RegExp

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 the 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 objects to get all the information about the current process of the 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.pid

1652

> 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 a 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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *