minte9
LearnRemember



Boolean

The if statement evaluates the expression in its parantheses.
 
/**
 * The if statement converts the result to a boolean
 * If the result is true executes the block of code
 * 
 * The ternary operator has three operands
 * It's the only operator witch has more than two operands
 * 
 * The two question marks operator ?? nullish coalescing
 * returns the first argument if it's not null/undefined
 */

let a = 20;
let x;

let b = a > 18 ? 'Yes' : 'No'; // Yes
let y = x ?? a; // 20

if (a > 18) {
    console.log("Allowed"); // Allowed
}
console.assert(b === 'Yes'); // pass
console.assert(y === 20);    // pass

Logical

In Javascript OR / AND have some powerful uses.
 
/**
 * In Javascript the OR operator is a bit trickier 
 * If an operand is not boolean, it's converted to a boolean
 * If all operands have been evaluated false, returns the last operand
 * 
 * OR return the first truly one
 * AND returns first falsy value 
 */

console.log( (true || false) === true );        // true
console.log( (false || false) === false );      // true
console.log( (1 || 0) === 1 );                  // true
console.log( (null || false || '0') === '0');   // true

let firstName;
let nickName;
let usename = firstName || nickName || 'Anonymous';

console.log(usename);       // Anonymous
console.log( 1 && 0);       // 0
console.log( 0 && 'abc');   // 0



  Last update: 201 days ago