DEV Community

8bit Programmer
8bit Programmer

Posted on

DAY 1 Js

How to import JS in HTML

<script src="app.js"></script>
Enter fullscreen mode Exit fullscreen mode

defer
attribute in script tag to ensure the HTML page loads before the js file or we can add the the script tag below the HTML file

How To import and Export in js

exportfile.js

export const apiKey="ASZFWXWFWGBWFKKG"
Enter fullscreen mode Exit fullscreen mode

importfile.js

import {apiKey} from "./exportfile.js"
Enter fullscreen mode Exit fullscreen mode

OR

By using the default method only one value can be exported from the file. you can give any name for the imported variable

exportfile.js

export default "ASZFWXWFWGBWFKKG"
Enter fullscreen mode Exit fullscreen mode

importfile.js

import apiKey from './exportfile.js'
Enter fullscreen mode Exit fullscreen mode

OR

Grouping

But you can have both in single file

exportfile.js

export default "agsdgadsh";
export const data="1234";
export let admin = true;
Enter fullscreen mode Exit fullscreen mode

importfile.js

import * as exp from './exports'
console.log(exp.default)
console.log(exp.data)
console.log(exp.admin)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)