Reputation: 1235
<script>
var Faizan = {
isSharp: true,
use: function(){
return this.isSharp = "Dangerous"//!this.isSharp;
}
};
console.log(Faizan.use());
console.log( !Faizan.isSharp, "Verify the value of isSharp has been changed." );
</script>
Output of the above script is 1.Dangerous 2.False 1st output is very sample as i expected it but second output is surprisingly false Why? when isSharp is change to Dangerous then Why !Faizan.isSharp Returning False?? isSharp value have changed
Upvotes: 0
Views: 42
Reputation: 895
As @Sacho said "!" is the logical NOT operator, so:
p | !p
-------------
true | false
false | true
In JavaScript non-empty string will evaluate to true if tested, see example below:
var simpleText = "This is simple text";
if (simpleText) {
console.log("The value is truthy");
} else {
console.log("The value is falsy");
}
// => The value is truthy
And empty string will evalute to false:
var emptyText = "";
if (emptyText) {
console.log("The value is truthy");
} else {
console.log("The value is falsy");
}
// => The value is falsy
You can read more about that here: http://www.sitepoint.com/javascript-truthy-falsy/
TL;DR
Your statement !Faizan.isSharp is equal to !true which is equal to false.
Upvotes: 2