Reputation: 305
I am importing library in my nodeJS code using const or var, want to know which is a better approach and memory efficient?
Option 1:
const _ = require('lodash');
Option 2:
var _ = require('lodash');
Which is better? Option-1 or Option-2 ?
Upvotes: 4
Views: 2892
Reputation: 10398
Using const
makes the most sense here. You don't want to accidentally overwrite the library you have imported as that may lead to hard to detect bugs. Marking it as const
will throw errors if you attempt to reassign to it (but doesn't make it immutable). Using const
may also allow js interpreters (or compilers in most cases now) to perform additional optimisations.
Upvotes: 10