Top companies Node Js Interview questions 1

1) Difference between var, let, and const?

VAR

var msg = "Hello world";

if(true){
    console.log("1 "+ msg)
}
console.log("2 "+ msg)

// output
// 1 Hello world
// 2 Hellow world

“var” is globally accessible, even if it is inside the scope like below example

if(true){
    var msg = "Hello world";
    console.log("1 "+ msg)
}
console.log("2 "+ msg)

// output
// 1 Hello world
// 2 Hellow world

Above both “var” declaration gives output same.

LET

But “let” variable type will not accessible from one to another.

if(true){
    let msg = "Hello world";
    console.log("1 "+ msg)
}
console.log("2 "+ msg)
//  msg is not defined

Here the above example will give the output “msg is not defined”.

console.log("2 "+ msg)
                  ^

ReferenceError: msg is not defined
    at Object.<anonymous>

CONST

const msg = "Hello";
if(true){
    msg = msg + " world";
    console.log("1 "+ msg)
}
console.log("2 "+ msg)

const stands for constant.
The constant variable will not able to modify the values. So, it throws an error “type error” like below.

    msg = msg + " world";
        ^

TypeError: Assignment to constant variable.
    at Object.<anonymous>
var Globally Accessable on both inside and outside of scope
let Not Globally Accessable. It is accessable only in inside of scope
const Not Globally Accessable on both inside and outside of scope. Once declare and assign the variable will not able to modify the values.

Check the next questions to crack the interviews.

How to create a server in node js with express.js

what is express.js?

Express.js is an npm(node package manager) package that is used to create an HTTP server listening on port 8080, which response to the client “Hello, User! This is the part of creating an HTTP server using express.js”. Express is a commonly-used web framework that is useful for creating HTTP APIs.

creating a server with express

Make sure you install nodejs from their official website with the stable or recommended version. Node Js are free for a lifetime.

Let, create a new folder, e.g. myApplication. Go into myApplication folder and make a new JavaScript file containing the following code(let’s name it server.js for example). Open the myApplication folder path in Terminal(command line), Then install the express module using the command below.

npm install --save express

or

sudo install --save express
It might ask your system user password to install the express package.

server.js File code

// Import the top-level function of express
const express = require('express');
// Creates an Express application using the top-level function
const app = express();
// Define port number as 8080, make sure no other server using this port, you can change any port number you want.
const port = 8080;
// Routes HTTP GET requests to the specified path "/" with the specified callback function
app.get('/', function(request, response) {
response.send('Hello, User! This is the part of creating http server using express.js');
});
// Make the app listen on port 8080
app.listen(port, function() {
console.log('Server listening on http://localhost:' + port);
});

Copy the above code and paste it in server.js file. From the command line, run the following command:
node server.js
Open your browser and navigate to http://localhost:8080 or http://127.0.0.1:8080 to see the response.