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

183
Views
Is there a way to pass a variable by its name in a function?

I am trying to pass a variable into a function by its name in javascript, as you can in python:

def myFunction(x=0, y=0):
    print(x, y)
myFunction(y=5)

>>> 0, 5

So far, I have been unable to find a way to do this:

function changeDir(x=0, y=0){
        console.log(x, y)
}
changeDir(y=5)

>>> 5, 0

This changes x, not y. Is there a way to change y without having to pass undefined as the first value?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You should pass an object param instead of individual params

function changeDir({ x = 0, y = 0 } = {}){ // x = 0 and y = 0 are default values
        console.log(x, y)
}
changeDir({ y: 5 }) //0,5
changeDir() //0,0
changeDir({x: 5}) //5,0
changeDir({y:5, x:10}) //10,5

about 4 years ago · Juan Pablo Isaza Report

0

You can pass your arguments as an object.

function changeDir({x=0, y=0}){
     console.log(x, y)
}
         
changeDir({y:5})
about 4 years ago · Juan Pablo Isaza 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!