Differences between COAP and HTTP
Differences between COAP and HTTP
April 4, 2023
What is Docker and Why do you need it?
What is Docker and Why do you need it?
April 18, 2023

April 11, 2023

Javascript Conversion to Boolean with “!!” Operator

The "!!" operator in javascript is used to convert a value to a boolean. In this example, it is used in the Account function to create a new property called "hasMoney" which is set to true if the "cash" property has a value, and false if it is 0 or undefined.
This can be seen in the example where the account with cash value of 125 has a "hasMoney" value of true and the empty account with a cash value of 0 has a "hasMoney" value of false.

function Account(cash) {
    this.cash = cash;
    this.hasMoney = !!cash;
}
var account = new Account(125);
console.log(account.cash);                // 125
console.log(account.hasMoney);            // true

var emptyAccount = new Account(0);
console.log(emptyAccount.cash);           // 0
console.log(emptyAccount.hasMoney);       // false
Javascript Conversion to Boolean with “!!” Operator
This website uses cookies to improve your experience. By using this website you agree to our Data Privacy Statement
Read more