!!expression for boolean

PaulaP

New Member
I was reading John Resig's Secrets of JavaScript Ninja and saw this code:\[code\] function Ninja(){ this.swung = false; // Should return true this.swingSword = function(){ return !!this.swung; }; }\[/code\]I know \[code\]!!\[/code\] is used to convert an expression into boolean. But my question is why does he use:\[code\]return !!this.swung;\[/code\]Isn't that redundant because \[code\]swung\[/code\] is already a boolean variable or am I missing something ?BTW here is full relevant code just in case:\[code\] function Ninja(){ this.swung = false; // Should return true this.swingSword = function(){ return !!this.swung; }; } // Should return false, but will be overridden Ninja.prototype.swingSword = function(){ return this.swung; }; var ninja = new Ninja(); assert( ninja.swingSword(), "Calling the instance method, not the prototype method.")\[/code\]
 
Top