samedi 28 avril 2018

'if' used in Javascript

var myGlobal = 10;
function fun1(){
oopsGlobal = 5; 
}
function fun2() {
  var output = "";
  if (typeof myGlobal != "undefined") {
    output += "myGlobal: " + myGlobal;
  }
  if (typeof oopsGlobal != "undefined") {
    output += " oopsGlobal: " + oopsGlobal;
  }
  console.log(output);
}
fun1();
fun2();

Console shows the results: 'myGlobal: 10,oopsGlobal: 5',but when I moved all '+' away the result changed into 'oopsGlobal: 5',I tried my best but still not know why this happen,thanks a lot!

Aucun commentaire:

Enregistrer un commentaire