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

161
Views
Pass variable into function to be defined

EDIT 2: Why has this very specific question been marked as a duplicate of this very conceptual one: What's the difference between passing by reference vs. passing by value?. If someone else had the same question as me, they wouldn't end up with this one on Google by searching for it and - even if they did - it wouldn't answer their question.

EDIT: Thanks everyone for your help so far. Pretty difficult stuff to understand from my point of view. The reason I'm doing this is that I've set up a function which I'm calling multiple times and in which I define a variable (a unique one with each call). I need to be able to refer back to each unique variable afterwards. Here is my actual attempted code below. What would the right way to do this be?

let newSeq1
let newSeq2
function sequenceClip(sample, length, sequenceVariable) {
        let currentPosition = Tone.Transport.position
        let whenToStart
        if (currentPosition === '0:0:0') {
            whenToStart = '0:0:0'
        } else {
            const barToStartOn = +currentPosition.slice(0, currentPosition.indexOf(':'))
            whenToStart = `${barToStartOn + 1}:0:0`
        }
        sequenceVariable = new Tone.Sequence((time, note) => {
            sampler.triggerAttackRelease(note, '4m', time)
        }, [sample], length).start(whenToStart);
    }

    loop1.addEventListener('mousedown', () => {
        sequenceClip("C3", '1m', newSeq1)
    })
    loop2.addEventListener('mousedown', () => {
        sequenceClip("C#3", '4m', newSeq2)
    })

How do I pass a variable into a function in Javascript to be assigned a value. E.g.:

Why does the variable not get assigned the value 5? And what's the way around this?

let a
function defineVariable(var2beDefined) {
   var2beDefined = 5
}
defineVariable(a)

console.log(a === 5)

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

0

Reassigning an identifier, by itself, never has any side-effects (in most circumstances) - the only change resulting from someIdentifier = someNewValue will be when other parts of the code reference that same someIdentifier.

If you want to pass in something to be assigned to, pass in a function which, when called, assigns to the outer variable.

let a;
function defineVariable(callback) {
  callback(5);
}
defineVariable(newVal => a = newVal);

console.log(a === 5)

The only time that assigning to an identifier will have side effects is if:

  • Inside a function with a simple argument list, you reassign one of the parameters (in which case the arguments object will be changed as well)
  • You're using ES6 modules, and you reassign an identifier that's being exported, in which case other modules that import it will see the change as well
about 4 years ago · Juan Pablo Isaza Report

0

You would typically write an initializer function that returns a value and assign that return value to the relevant global variable. For example:

let newSeq1
let newSeq2

function sequenceClip(sample, length, sequenceVariable) {
    let currentPosition = Tone.Transport.position
    let whenToStart

    if (currentPosition === '0:0:0') {
        whenToStart = '0:0:0'
    } else {
        const barToStartOn = +currentPosition.slice(0, currentPosition.indexOf(':'))
        whenToStart = `${barToStartOn + 1}:0:0`
    }

    return new Tone.Sequence((time, note) => {
        sampler.triggerAttackRelease(note, '4m', time)
    }, [sample], length).start(whenToStart);
}

loop1.addEventListener('mousedown', () => {
    newSeq1 = sequenceClip("C3", '1m')
})

loop2.addEventListener('mousedown', () => {
    newSeq2 = sequenceClip("C#3", '4m')
})

Note that both newSeq1 and newSeq2 will be undefined until the first mousedown/mouseup events.

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!