Tutorial
Fundamentals and Features

Node js Fundamentals and Features

🎉

Read This on Github

This article is available on Github. You can read it there and contribute to it.
Github Link
Any Issue ?

⭐ Fundamentals and Features

Explain

For NodeJs you need to have a basic knowledge of Javascript. If you are new to Javascript then you can check out my tutorial on Javascript (opens in a new tab)

Some Important Notes

  1. Suppose you make a file app.js and another file index.js and you want to import the app.js file like this
app.js
export const name = "Subham Maity"
export const age = 20
index.js
import {name,age} from "./app.js"

And in your terminal you run node index.js then you will get an error like this

SyntaxError: Cannot use import statement outside a module

This is because NodeJs doesn't support the import and export syntax. So you have to use the module.exports and require syntax.

app.js
module.exports = {
    name: "Subham Maity",
    age: 20
}
index.js
const {name,age} = require("./app.js")
 
console.log(name,age)

Now if you run node index.js then you will get the output like this

Subham Maity 20
  1. You can even create a function in app.js and export it and then import it in index.js and use it.
app.js
module.exports = {
    add: (a,b) => a+b
}
index.js
const app = require("./app.js")
 
console.log(app.add(2,3))

Now if you run node index.js then you will get the output like this

5
⚠️

What is filter in Javascript ?

  • If you have an array like this
const arr = [1,2,3,4,5,6,7,8,9,10]

and you want to traverse through the array and get the elements which are greater than 5 then you can use the filter method.

const arr = [1,2,3,4,5,6,7,8,9,10]
 
const greaterThan5 = arr.filter((element) => element > 5)
 
console.log(greaterThan5)
 

Now if you run node index.js then you will get the output like this

[ 6, 7, 8, 9, 10 ]
  • If you have an array like this
const arr1 = [1,2,3,4,4,4,5,6,7,8,9,10]

and you want to traverse and find the value that is repeated the most then you can use the filter method.

const arr1 = [1,2,3,4,4,4,5,6,7,8,9,10]
 
const mostRepeated = arr1.filter((element) =>
{
    return element === 4
})
 
console.log(mostRepeated)
 

Even you can do greater than 5 and less than 8

const arr1 = [1,2,3,4,4,4,5,6,7,8,9,10]
 
const mostRepeated = arr1.filter((element) =>
{
    return element > 5 && element < 8
})
 
console.log(mostRepeated)