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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With