Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Nikhil Chandra Roy
Nikhil Chandra Roy

Posted on

     

JavaScript import export module features Tutorials

Hello guys,
In this post, we will learn javascript import, export module features shortly.

1st test.

we will create below files

  • index.html
  • main.js
  • alert.js

so, in index.html we will do like this,

<!doctype html><html lang="en">  <head>    <title>Title</title>    <!-- Required meta tags -->    <meta charset="utf-8">    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">  </head>  <body>    <script src="main.js" type="module"></script>  </body></html>
Enter fullscreen modeExit fullscreen mode

for script has type attribute where the script get it by module.

In main.js file we will do

import {Alert} from './alert.js';function Main(){    Alert()}Main()
Enter fullscreen modeExit fullscreen mode

and in alert.js file

export function Alert(){    alert("I am alert")}
Enter fullscreen modeExit fullscreen mode

so when we reload the page we will see that our module alert.js working with alert("I am alert")
now, we will do some example import,export way for practice and doing.

In alert.js when we don't use default along with function we have to remove the curly bracket in main.js
like

In alert.js

export default function Alert(){    alert("I am alert")}
Enter fullscreen modeExit fullscreen mode

In main.js

import Alert from './alert.js';
Enter fullscreen modeExit fullscreen mode

and without default, we have to

In alert.js

export function Alert(){    alert("I am alert")}
Enter fullscreen modeExit fullscreen mode

In main.js

import {Alert } from './alert.js';
Enter fullscreen modeExit fullscreen mode

for multiple function we may use "as" along with function like

import {Alert as nikhil}  from './alert.js';
Enter fullscreen modeExit fullscreen mode

we can also use variable as well as function for module like below

In alert.js

export let time = new Date().toLocaleTimeString();
Enter fullscreen modeExit fullscreen mode

In main.js

import {Alert as nikhil, time}  from './alert.js';function Main(){    nikhil()    console.log(time)}Main()
Enter fullscreen modeExit fullscreen mode

This is our short tutorials to use the module in javascript,
I will continue to post for short time to learn small and small things.
If you like this small post, don't forget to comment your opinion.
Thanks.

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
chandragie profile image
chandragie
A learner of code.
  • Location
    Indonesia
  • Work
    Senior Web Developer & IT Analyst
  • Joined

Thanks for this short, straight forward but so useful info!

CollapseExpand
 
nikhilroy2 profile image
Nikhil Chandra Roy
Front-End Web Developer with JavaScript, ReactJs and Backend Python(Django)
  • Location
    Bangladesh
  • Joined

welcome,

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Front-End Web Developer with JavaScript, ReactJs and Backend Python(Django)
  • Location
    Bangladesh
  • Joined

More fromNikhil Chandra Roy

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp