Three ways to declare variables in javascript
Feature | var | let | const |
---|---|---|---|
redeclare | Yes | No | No |
Update | Yes | Yes | No |
Scope | functional | Block | Block |
If you wants to exports only one variable / object
module.exports = my_object;
exports = my_object; // will not work
If you wants to exports multiple things as object
module.exports.my_variable = my_variable;
module.exports.my_object = my_object;
module.exports.my_function = my_function;
module.exports = {
my_variable,
my_object,
my_function
}
exports.my_variable = my_variable;
exports.my_function = my_function;
exports.my_object = my_object;
// Below will not work
exports = {
my_variable,
my_object,
my_function
}
- We can not use both
module.exports
andexports
at once in our file (import).
- We can use
export default
only once in our file.
export default my_variable
- If we want to export more than 1 variable / object then we can use
export
export { my_variable, my_function };
- We can use both
export default
andexport
at once in our file.
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
- Emulating private methods with closures
- spread operator, Object.assign(), JSON.parse(JSON.stringify()) do deep copy on array and object
- spread operator, Object.assign() does not do deep copy of nested object element but JSON.parse(JSON.stringify()) do deep copy of nested objects element on object
- spread operator, Object.assign(), JSON.parse(JSON.stringify()) do deep copy of nested array element on array