Ad
  • Custom User Avatar

    I don't quite understand how logical IF operators are used here. Can someone explain?

  • Custom User Avatar
  • Custom User Avatar

    @nabramow you could test it yourself in the console.
    All it does is convert it to a number, convert to Boolean, then finally convert it back to a number
    (operand -> number -> Boolean -> number).

    Unary Plus or "+" converts an operand to a number, provided it is in an integer format. E.G. +"5" => 5. The first use of it converts a string integer to an integer. Then second use of it converts the Boolean (true or false) to its integer equivalent; false = 0, true = 1.

    Logical NOT operator or "!" will return true if the operand is empty or non-functional, and return false if it's not empty or it's functional. In terms of integers, this means it will return false if the integer is not equal to 0. It does this by negating the operand. E.G. !5 => false.

    var num = "5";
    
    num = +num    ->  5
    
    num = !num    ->  false
    
    num = +num    ->  0
    
    
    therefore +!+"5"  => 0
    
  • Custom User Avatar

    let's have this in steps:
    str2 is a string but contains a number.
    +str2 converts that strign to a number.
    !+str2 converts that number to a boolean and inverts. This way it becomes either false if non-zero or true if zero.
    +!+str2 converts that boolean back to a number, now we have either 0 or 1.

    So this +!+str2 is just a way to put "give me 0 if str2 is not "0", otherwise give me 1".

  • Custom User Avatar

    Would someone mind explaining what the +!+ does? I'm not quite sure what to google for more info.