Node JS Interview Questions

Oct 13, 2022
100 views
21 questions

About Node.js

Node.js is a kind of open-source platform which is used for executing JavaScript by using the various codes prepared using the Java programming language outsides any of the browsers used.

Key Features of Node Js

  Open Source
  Asynchronous and Event-Driven
  Very Fast
  Highly Scalable
  Massive Speed Gains
  Cross-Platform
  Object-Oriented
  Synchronous Code Executions

Node JS Interview Questions for Freshers

Node.js is an open-source, server-side Javascript runtime platform that is built on Google Chrome's V8 Javascript Engine. Node.js is developed by Ryan Dahl and written in C, C++, JavaScript programming language.

 

Node.js is an open-source, server-side Javascript runtime platform that is built on Google Chrome's V8 Javascript Engine. Node.js is developed by Ryan Dahl and written in C, C++, JavaScript programming language.

 

let, const and var in Node JS

Var – a variable declared with var can be used in the entire program. It is declared as the variable is available to access within that function. Also, the variable can be updated anytime.

Let – Let is considered to be a block with curly brackets. A variable with let and curly brackets (block), means that the variable can be used only within the specified block.

Const – Variables that do not need to get updated in the complete lifecycle of the program are declared with const.

Middleware is a layer of software that is allowed to lie between operating systems and applications. It acts as a pipe to transfer data between the application and operating systems.

In Node.js middleware are functions that sit between the request object ( req ), the response object ( res ). It acts as a filter that has access to both the request and response object.

Below are a few examples of middleware

  • Database middleware
  • Application server middleware
  • Message-oriented middleware
  • Web middleware

Promises are specifically designed pattern used to reduce the practice of unintuitive call backs. Promises are generally used with ‘then’ function in the code.

Promises are designed to execute two functions.

  • When the promises are resolved and
  • When the promises are rejected.

A queue is specifically used to control the execution flow. Also, the queue is used to get a solution for a specific problem.

Node.js is an application especially called as single-threaded. They are used to provision concurrency by the concept of event and callbacks.

When a task is getting completed, node thread by using the event loop triggers the next function to get executed.

When the event loop triggers, there is an event listener ready to execute the next function. These structural loops are called as event driver architecture.

When there is a completion of any single-threaded we need to tell callbacks what to do next.

Promises are designed in such a way that it comes back with an object (alert) and then we will tell promises about what to do next when there is asynchronous task completion.

Promises are also known as asynchronous callbacks. They are better callbacks because they make error handling across multiple asynchronous calls more effortless than callbacks.

Callback Hell is also known as the pyramid of doom. It is an anti-pattern seen in the code of programmers who are not wise in the ways of asynchronous programming.

The purpose of Forever is to keep a child's process running continuously and automatically restart it when it exits unexpectedly. It starts a script as a daemon.

Node.js is based on single-threaded architecture because it has one call stack and one memory heap. As expected, it executes code in order and must finish executing a piece of code before moving onto the next

The difference between Node. js and Ajax can be stated as, Ajax or Asynchronous JavaScript and XML is a client-side technology, often used for updating the contents of the page without refreshing it. Whereas Node. js is Server-Side JavaScript, used for developing server software.

REPL stands for Read-Eval-Print-Loop.In Node.js REPL is a virtual environment that allows developers to read, write and execute JavaScript code. To launch Node REPL simply open command prompt on windows and type node.

Node REPL

The Buffer class in the Node JS is used to store raw data in a raw memory allocation outside the V8 heap. It is used to handle the octet streams while dealing with the TCP streams. A Buffer class is like an array but stores the stream data in 7667ytghfixed-size, raw memory allocation. The buffer class is within a global scope so users don’t need to use the require('buffer').Buffer.

As the name suggests, global objects are available in all the modules as they are global in nature. Developers can use it directly in their application instead of including it to use it. Some of these global objects are not actually in the global scope but in the module scope.

Some important global objects are:-  __dirname, __filename, console, process, buffer, setImmediate(), setInterval(), setTimeout(), clearImmediate(), clearInterval, clearTimeout.

These global objects can be modules, functions, strings, and objects.

The Zlib module in the Node JS is used to compress and decompress the files. This module is implemented using Gzip and Deflate/Inflate. It provides the way for zipping and unzips files. This module comes with a lot of built-in properties and methods for working with compression and decompression. Before using the module,

you should include it in your file by using the following line.

var Zlib = require('zlib')

NPM (Node Package Manager) is a JavaScript Package manager. It consists of a command-line, online database, public, and private packages. You can download the required package for your application by using the npm. It is free to use registry and the largest software registry in the world. Released in 2010, npm is written in JavaScript. The npm has over 477,000 packages present in the main npm registry.

To uninstall a dependency using the npm, you can simply use the following statement in the npm command line.

npm uninstall <package-name>

The main objective of piping is to read data from a readable stream of data, and it functions to write the same data into a write stream.

In the node applications, the stream of data between the two arguments is piped together. This is done by using the pipe method.

The Buffer module is used by the Node JS for implementing buffer based operations. Buffers are a chunk of memory created outside of v8 heap and are implemented using a JavaScript typed array. It is a globally available module to enable the interaction with octet streams in TCP streams.

In NodeJS, chaining helps to chain asynchronous fusions.

Chaining process consists of

  • Parallel mode – The chaining runs the asynchronous functions in parallel to the other functions.
  • Serial mode – The chaining does not run the asynchronous function if any other functions are engaged.

Difference between return and callback

When we use call back function, if there is an asynchronous function, it does not return with a value. Instead, we have to identify and make the further process.

When we use a promise function, it returns with an alert (value) to us if there is any asynchronous function. So immediately we could able to react to it.

Share this post