const vs let when calling require

All we need is an easy explanation of the problem, so here it is.

As io.js now supports ES6 you are finally able to use the const and let keywords. Obviously, let is the successor of var, just with some super-powers.

But what about const? I know, of course, what “constant” means, but I was wondering when to use it (regarding best practices).

E.g., if I create a module that requires another module, I could write:

'use strict';

const util = require('util');

const foo = function () {
  // Do something with util

module.exports = foo;

Basically I’ve replaced every occurence of var with const. Generally speaking, I think that this is okay, but if I follow this pattern, it leaves me with way more uses of const than let, as most variables aren’t “variables” in a literal sense.

Is this good style? Should I rather go for let? When should I choose const over let?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

const can be normally used when you don’t want your program

  1. to assign anything to the variable

    "use strict";
    const a = 1;
    a = 2;

    will produce TypeError: Assignment to constant variable..

  2. to use the variable without explicitly initializing.

    "use strict";
    const a;

    will produce SyntaxError: Unexpected token ;

Simply put, I would say,

  • use const whenever you want some variables not to be modified

  • use let if you want the exact opposite of const

  • use var, if you want to be compatible with ES5 implementations or if you want module/function level scope.

Use let only when you need block level scoping, otherwise using let or var would not make any difference.

Method 2

I have the same feeling that you’re describing. A big percentage of declared variables in my code tend to be constant, even objects and arrays. You can declare constant objects and arrays and still be able to modify them:

const arr = [];
// [ 1 ]

const obj = {};
obj.a = 1;
// { a: 1 }

AFAIK ES6 modules do not require variable declarations when importing, and I suspect that io.js will move to ES6 modules in a near future.

I think this is a personal choice. I’d always use const when requiring modules and for module local variables (foo in your example). For the rest of variables, use const appropriately, but never go mad and use const everywhere. I don’t know the performance between let and const so I cannot tell if it’s better to use const whenever possible.

Method 3

Performance test const vs let usage on require for Node.js 6.10:

  { name: 'test 1', fn: function() { 
    const path = require('path');
  { name: 'test 2', fn: function() { 
    let path = require('path');

test 1 …. 2,547,746.72 op/s
test 2 …. 2,570,044.33 op/s

Method 4


let is a successor of var keyword with imposed restrictions. These restrictions make the chances of making fewer mistakes and with added security. Basically, it has block scope i.e. it is only available to the block where it is declared. In other words, variables cannot be accessed outside the block it has been declared.

const accessibility is also inside block scope. But once initialized cannot be re-initialized. This can be tricky in the case of Array, Objects

What does array initialization mean?

let arr = [1, 2, 3, 4, 5];

arr contains the reference (address or pointer) of the first element of the array. That is the reason why this never holds good.

let arr1 = [1, 2, 3, 4, 5];
let arr2 = [1, 2, 3, 4, 5];
if (arr1 == arr2)
    console.log("Both arr1 and arr2 are same");

This shall never print Both arr1 and arr2 are same in the console. Though they looks same in all aspects. But if we see then arr1 and arr2 are pointing to different locations in the memory. The same concept goes with objects. For example:

let obj1 = {name:'John', age:'22'}
let obj2 = {name:'John', age:'22'}

if (obj1 == obj2)
     console.log("Both obj1 and obj2 are same");

obj1 and obj2 are pointing to different memory locations. So the console.log statement shall never run.

If we use const declaration for the array or object, then according to the definition it cannot be re-initialized. But actually the identifier assigned to the array or object is a memory pointer (address). The array or object can be modified (mutated) accordingly.

Answer to the question:

const util = require('util')

This type of declaration makes sure that accidentally util is not declared the second time. This makes the code less error-prone i.e. reduce the chances of making mistakes. const declaration in function makes it less error-prone. As redeclaring, it throws an error.

For instance, say there is a function you declared factorial that is responsible for finding factorial of a number. Let us consider this example:

const factorial = (n) => {
     let fact = 1;
     for(let i=1;i<=n;i++)
         fact *= i;
     return fact;

const factorial = 5;

Here it will throw an error. const makes the use of the identifier factorial unique. i.e. a function that takes an input and finds factorial of it.

This helps in making lesser mistakes that are very difficult to debug.

If an array or object is declared const, then the array or object can be modified (mutated) but any other identifier cannot use the same name that is declared using const.

I hope this helps.

Method 5

const: scope: block. reassign: not allow. declaration+initialization: must

let: scope: block. reassign:allow. declaration+initialization:optional

var: scope: global & functional. reassign: allow. declaration+initialization:optional.

According to w3schools use always use const is a good practice.

const vs let when calling require

So my preference:

  1. const
  2. let
  3. var

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply