-3
function switchValue (a, b) {
  return [b,a] = [a,b]
}

var a = 'computer'
var b = 'laptop'
switchValue(a, b)

console.log("a = " +a) 
console.log("b = " +b)

how to change this variable, that output :

a = laptop
b =  komputer 

please help me

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
  • Possible duplicate of [How to swap two variables in JavaScript](https://stackoverflow.com/questions/16201656/how-to-swap-two-variables-in-javascript) – Jared Smith Dec 13 '18 at 12:03

2 Answers2

0

Try this

 function switchValue(a, b) {
    let c = a;
    let a = b;
    let b = c;
    return [a, b];    
}
Jain
  • 1,209
  • 10
  • 16
0

You can do it like this but generally it's not advisable to use global variables

var a = 'computer' 
var b = 'laptop' 

function switchValue (val1, val2) { 
  let c = val1;
    a = val2;
    b = c;
 }

     switchValue(a, b)

    console.log("a = " + a);
    console.log("b = " + b);

Without global variables

function switchValue (a, b) { return [b,a] }

var a = 'computer' 
var b = 'laptop' 

 let [A,B] = [...switchValue(a, b)]

console.log("a = " + A);
console.log("b = " + B);
Code Maniac
  • 37,143
  • 5
  • 39
  • 60