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

257
Views
How to make function parameter constant in JavaScript?

What I want to do is to use as many immutable variables as possible, thus reducing the number of moving parts in my code. I want to use "var" and "let" only when it's necessary.

This won't work:

function constParam(const a){
    alert('You want me to '+a+'!');
}

Any ideas?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Function parameters will stay mutable bindings (like var) in ES6, there's nothing you can do against that. Probably the best solution you get is to destructure the arguments object in a const initialisation:

function hasConstantParameters(const a, const b, const c, …) { // not possible
    …
}
function hasConstantParameters() {
    const [a, b, c, …] = arguments;
    …
}

Notice that this function will have a different arity (.length), if you need that you'll have to declare some placeholder parameters.

over 4 years ago · Santiago Trujillo Report

0

You can't make a parameter const. Use it as the initial value of a local variable:

function constParam(a) {
    const const_a = a;
    ...
}

Note also that const is only supported in Internet Explorer as of IE11. See this compatibility table

over 4 years ago · Santiago Trujillo Report

0

There is no way to force a parameter to be immutable in JavaScript. You have to keep track of that yourself.

Just write in a style where you happen not to mutate variables. The fact that the language doesn't provide any facilities to force you to do so doesn't mean that you can't still do it anyway.

over 4 years ago · Santiago Trujillo 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!