Naming named imports

You can also change the name of a named export in the module to which you're importing it. This is achieved by using the as keyword:

import { takeSquareAndAdd2 as myFunc } from './module.js';console.log(myFunc(3))

This will still produce the output 11. This is essential when you have long module names or names that might conflict with your base code/other imported modules.

Similarly, you can rename some named imports, and leave the rest as it is:

import { takeSquareAndAdd2 as myFunc, yourName } from './module.js';console.log(myFunc(3)) // 11console.log(yourName("Mehul")) // Your name Mehul is a nice name

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.