0

This works:

//a.js
export let x = false;

export function toggle_x() {
  if (x) {
    x = false;
  } else {
    x = true;
  }
}

//b.js

import {x, toggle_x} from {'/a.js'}
toggle_x()
console.log(x) //outputs true, great!

This does not work:

//a.js
export let x = false;

export function toggle(v) {
  if (v) {
    v = false;
  } else {
    v = true;
  }
}

//b.js

import {x, toggle} from {'/a.js'}
toggle(x)
console.log(x) //outputs undefined

Why? I want to create a function to toggle between true/false the entered variable.

Thanks!

Gabriel
  • 5,453
  • 14
  • 63
  • 92
  • 2
    `import {x, toggle} from {'/a.js'}` is invalid syntax. – Andrew Li Nov 17 '18 at 15:32
  • 1
    You cannot do what you're describing. JavaScript is a pass-by-value language. In your second code sample, `v` completely local to the toggle function, so changes to its value won't be reflected anywhere else. – Pointy Nov 17 '18 at 15:33

0 Answers0