If I give my variables an alias, can I use both the variable's alias and its original name?


#1

Question

If I give my variables an alias, can I use both the variable’s alias and its original name?

Answer

We will not be able to use both the original variable name and the variable’s alias if we have given a variable name an alias; we will only be able to use the alias.

For example:
moduleToExport.js:

const myObj = {
  prop1: 'prop 1',
  prop2: 'prop 2'
};

export {myObj as myObjAlias};

main.js - importing the original variable name:

import {myObj} from './moduleToExport.js'; //we see `Uncaught SyntaxError: The requested module './moduleToExport.js' does not provide an export named 'myObj'` in the console

main.js - importing the alias given to the variable name:

import {myObjAlias} from './moduleToExport.js'; //importing the alias however, will not give us an error in the console

FAQ: Intermediate JavaScript Modules - Export as