-1

i try to reading code of pandajs game engine and according to this code from github :

playMusic: function(name, loop) {
    var volume = this.musicMuted ? 0 : this.musicVolume;

    if (typeof loop === 'undefined') loop = true;

    if (this.currentMusic) this._stop(this.currentMusic);

    this.currentMusic = this._play(name, !!loop, volume);
    this.currentMusicName = name;

    return !!this.currentMusic;
}

and it's return !!this.currentMusic i try to figure it out but i cant understand why he use it. for example this code :

function a(arg){
  console.log(!!arg);
}

a(true)
a(false)

if i pass true it's print true and if i pass false it's print false , so why not just return this.currentMusic instead of !!this.currentMusic or in my example just console.log(arg) instead of console.log(!!arg)

MBehtemam
  • 6,516
  • 11
  • 56
  • 98

1 Answers1

2

it coerces the object to boolean, if it was null, undefined etc it would be false else true

let x = null;
let y = 1;
console.log(!!x);
console.log(!!y);
marvel308
  • 9,593
  • 1
  • 16
  • 31