Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is assignation not working in lambda function

Having

const assign = (a, fn) => {a = fn(a)}

and for instance

let a = 5
const double = (a) => a*2
assign(a, double)
a

still evaluates to 5. Same goes for Strings (which are objects).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Function parameters are passed by value.

You can instead make a an object, and modify its property.

const assign = (a, fn) => {
  a.value = fn(a)
}
let a = {value: 5}
const double = (a) => a.value * 2
assign(a, double)
console.log(a.value)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading