-1

I would like to shorten the amount of lines in my js. I have my json on top of the file. I would like to store that in a separate file. In php you would just do an include statement is there something like this but for js?

Neiko101
  • 7
  • 1
  • Does this answer your question? [How to import a json file in ecmascript 6?](https://stackoverflow.com/questions/34944099/how-to-import-a-json-file-in-ecmascript-6) – Shubham Sharma Dec 10 '19 at 03:44

1 Answers1

0

There are several ways you can communicate between js files. in es5

//FOR EXPORT use module.exports

eg. module.exports= any content(function, object, array).

//FOR IMPORT use require method

eg. const xyz = require('.path_to_your_file) //.js extension is optional

//now exported content will be available in xyz.

In es6 we have named export and default export

// FOR **default export** use export default 
// eg. export default (any content [array,object,function])
// NOTE:- you can have only one default export in a file

// FOR named export use export 
// eg. export (any content [array,object,function])
// NOTE:- you can have multiple export in a file

///####################################
// FOR importing **default exported** content use following syntax
// import ABC from 'source file';

 //now exported content will be available in ABC.

// FOR importing **named exported** content use following syntax
// import {exported_name} from 'source file'; // see object destruct in es6 for detail
// as we can have multiple named export we can import multiple content using comma separated syntax and using.
// import { export1,export2} from 'source file';

also you can combine all the named exports in single import name as below.

//import * as ABC from 'source file';

here all the named export will be available in ABC object and you can access through dot or bracket notation.

kisor
  • 464
  • 5
  • 22