Skip to content

Instantly share code, notes, and snippets.

@abcdeepakr
Created April 21, 2021 09:06
Show Gist options
  • Save abcdeepakr/8dc48d2f38e9fb5d17c1a40673a4ad5d to your computer and use it in GitHub Desktop.
Save abcdeepakr/8dc48d2f38e9fb5d17c1a40673a4ad5d to your computer and use it in GitHub Desktop.
Import and Export Javascript Functions/Files
// Let us assume that this is the file you are working with and you need some arithmetic functions
//below is the import statement
const math = require("math.js")
let num1 = 10
let num2 = 20
console.log(math.add(num1,num2)) //math.add is how we will be accessing the add functions from the math.js file
make sure that you pass two arguments, since add function requires two arguments.
// parent file (library/module. We will export the functions of this file
const add = (x,y) => x+y // This function will add two numbers and return it
const subtract = (x,y) => x-y // this function will subtract two numbers and return it
//similarly create as many function as you want and export them in the belwo mentioned syntax
module.exports = {add, subtract} //these two functions will be exported.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment