Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
Const Functions to an array from other file

I have 2 functions to const in my index.js:

const funcE = require("../Functions/cmdError")
const funcH = require("../Functions/cmdHelp")

When I call functions must be like this:

funcE.cmdError(args)
funcH.cmdHelp(args)

I want join 2 const lines into 1 line and how to call these funtions?

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

You have 3 possibilities to declare your constants in one line.

Option 1

The simplest way:

const funcE = require("../Functions/cmdError"); const funcH = require("../Functions/cmdHelp");

Option 2

Use a comma and don't use const for the second declaration.

const funcE = require("../Functions/cmdError"), funcH = require("../Functions/cmdHelp");

Option 3

Use Destructuring assignment.

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

const [funcE, funcH] = [require("../Functions/cmdError"), require("../Functions/cmdHelp")];

This is how you can invoke your functions in all cases:

funcE.cmdError(args)
funcH.cmdHelp(args)

This is what you can do, if you want to have 1 constant only:

const func = [require("../Functions/cmdError"), require("../Functions/cmdHelp")];

Invoke the functions with: func[0].cmdError(args) and func[1].cmdHelp(args)

OR

you could also declare an object.

const functions = {
  funcE: () => require("../Functions/cmdError"),
  funcH: () => require("../Functions/cmdHelp")
}

Invoke the functions with: functions.funcE() and functions.funcH()

about 4 years ago · Santiago Gelvez Report

0

You can also reach your goal by doing this:

Create a new file, for example, index.js. In this index.js file write your "requires" and export them.

const funcE = require("../Functions/cmdError");
const funcH = require("../Functions/cmdHelp");

module.exports = {
  funcE,
  funcH
}

Then you can require the index.js file where you want and access these functions.

about 4 years ago · Santiago Gelvez Report

0

I figured out how to do it already, thanks everyone.

const func = [require("../Functions/cmdHelp"), require("../Functions/cmdError")]

and call it:

func[0].cmdHelp(args)
func[1].cmdError(args)
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!