Truthy and Falsy Values

  • 5 falsy values –> 0, "", undefined, null, NaN
  • Everything else is True
// false
console.log(Boolean(0));
console.log(Boolean(undefined));
console.log(Boolean(''));

// true
console.log(Boolean('Jonas'));
console.log(Boolean({}));

PDF

  • Beware of Objects & Arrays in Comparisons!
  • Truthy and Falsy Values
    Page: /

Important Operations

Mixed Number & String Arithmetic Operators

  • Expression in JS executes left to right

String + Number = String

console.log("3"+88) 
// "388"
console.log("" + 1 + 2) 
// "12"
console.log(1 + 2 + "") 
// "3"
console.log(2 + 2 + '1' ); 
// "41" and not "221"
  • unary+ convertes both values to numbers before the binary plus
console.log(+'1'+ + '2') // 3

Other than ‘+’ string are converted to number

  • String * String = Number
  • String / String = Number
  • String - Number = Number
console.log("" - 1 + 3)  
// 2
console.log( 2 - '1' );
// 1
console.log('zz' - 1 + 0) 
// Nan 
console.log( '6' * '2' ); 
// 12
console.log( '6' / '2' ); 
// 3	

Mixed Number & String Comparison Operators

  • Strings can also be compared with greater than (>) or lower/ smaller than (<) operators.
  • JavaScript compares strings based on standard lexicographical ordering, using Unicode values.

JavaScript always looks at the first character and only considers other characters if the first character is similar. In addition, capital characters are considered to be smaller than lowercase characters

console.log('6' < '2' ); 
// false
console.log('ab' > 'aa') 
// true
console.log('a' > 'B') 
// true
console.log('a' > 'b') 
// false

Beware of Objects & Arrays in Comparisons!

  • Objects and arrays are kind of special in JavaScript!
  • In below example
    • person1 and person2 are reference-type
    • They store address/location of two different object
    • So person1 and person2 are not equal
    • Whereas person3 and person1 stores the same address/location
    • So, person1 and person3 they are equal
// Object
const person1 = {name: 'Max'}
const person2 = {name: 'Max'}
console.log(person1 == person2)
// false
console.log(person1 === person2)
// false

const person3 = person1
console.log(person1 == person3)
// true
console.log(person1 === person3)
// true
// Array
const hobbies1 = ['Sports', 'Cooking']
const hobbies2 = ['Sports', 'Cooking']
console.log(hobbies1 == hobbies2)
// false
console.log(hobbies1 === hobbies2)
// false

const hobbies3 = hobbies1
console.log(hobbies1 == hobbies3)
// true
console.log(hobbies1 == hobbies3)
// true

Operator precedence

  • Arithmetic operator precedence is normal math operation(BODMAS rule)
  • && has higher precedence than ||
console.log(2 + 3 * 4 / 2)
// 8
console.log(5!=5 && 3>6 || 10<5)
// false

const now = 2037;
console.log(now - 1991 > now - 2018);
// true

let y;
x = y = 25 - 10 - 5; 
console.log(x, y);
// 10 10
console.log(2 + 3 / 3); 
// 2 + (3/3) = 3

Comparisons and Strict equality

alert( 'Z' > 'A' ); // true
alert( 'Glow' > 'Glee' ); // true
alert( 'Bee' > 'Be' ); // true

alert( '2' > 1 ); // true, string '2' becomes a number 2
alert( '01' == 1 ); // true, string '01' becomes a number 1

alert( 0 === false ); // false, because the types are different
Strange result:
Values null and undefined equal for '==' on each other and do not equal any other value.
    alert( null == undefined ); // true 

Comparisons convert null to a number, hence treat it as 0, while equality check does not.
    alert( null > 0 );  // (1) false
    alert( null == 0 ); // (2) false
    alert( null >= 0 ); // (3) true

    alert( undefined > 0 ); // false (1)
    alert( undefined < 0 ); // false (2)
    alert( undefined == 0 ); // false (3)

Javascript: difference between a statement and an expression?

  • Expression
    • Yields a value and can be stored in a variable
    • Used right-side og equal sign
    • Every expression is an statement
  • Statement
    • if-else –> is statement
    • Every statement is not expression