Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const vs. var in nodejs require statement

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 ?

like image 306
Mrugesh Vaghela Avatar asked Jan 26 '23 09:01

Mrugesh Vaghela


1 Answers

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.

like image 192
James Coyle Avatar answered Jan 28 '23 21:01

James Coyle