Brainfuckers Seen Among JavaScript Developers

    JavascriptPhp
    // if ( 0 ) не выполнится 
    // if ( 0 ) не выполнится 
    if ( 2 ) выполнится 
    if ( 2 ) выполнится 
    if ( -1 ) выполнится 
    if ( -1 ) выполнится 
    if ( false == 0 ) выполнится 
    if ( false == 0 ) выполнится 
    if ( false == '0' ) выполнится 
    if ( false == '0' ) выполнится 
    if ( false == '00' ) выполнится 
    // if ( false == '00' ) не выполнится 
    if ( false == '0x0' ) выполнится 
    // if ( false == '0x0' ) не выполнится 
    - we see that JavaScript parses a string with a number more carefully. We go further
    if ( 'bla bla bla' ) выполнится 
    if ( 'bla bla bla' ) выполнится 
    // if ( false == null ) не выполнится 
    if ( false == null ) выполнится 
    - amazing, but in JavaScript false is not null


    if ( false == !!null ) выполнится 
    if ( false == !!null ) выполнится 
    - but if we have denied null twice - javascript behaves like PHP
    // if ( false == undefined ) не выполнится 
    if ( false == $undefined ) выполнится 
    - oops! undefined is also not false in JavaScript
    if ( false == !!undefined ) выполнится 
    if ( false == !!$undefined ) выполнится 
    - double negation changes things again
    // if ( false == 'false' ) не выполнится 
    // if ( false == 'false' ) не выполнится 
    if ( true == 1 ) выполнится 
    if ( true == 1 ) выполнится 
    // if ( true == 11 ) не выполнится 
    if ( true == 11 ) выполнится 
    - Another feature - in PHP, any number other than zero is true. In JS, only one
    if ( 11 ) выполнится 
    if ( 11 ) выполнится 
    - but if we pass the number directly to if - then in both languages ​​everything that is nonzero is true
    // if ( true == '0' ) не выполнится 
    // if ( true == '0' ) не выполнится 
    if ( '0' ) выполнится 
    // if ( '0' ) не выполнится 
    - Perhaps the most insidious difference between JavaScript and PHP. Further
    // if ( true == 'true' ) не выполнится 
    if ( true == 'true' ) выполнится 
    - also an important difference - PHP casts a string to a string, and JS casts a string to a string
    // if ( true == -1 ) не выполнится 
    if ( true == -1 ) выполнится 
    - again, the difference is that in JS true is reduced to a number (it turns out 1) and is compared with -1. And in PHP -1 it is cast to a buoy (it turns out true)
    if ( 'bla bla bla' ) выполнится 
    if ( 'bla bla bla' ) выполнится 
    // if ( undefined ) не выполнится 
    // if ( $undefined ) не выполнится 
    if ( undefined == null ) выполнится 
    if ( $undefined == null ) выполнится 
    // if ( 1 == 2 ) не выполнится 
    // if ( 1 == 2 ) не выполнится 
    if ( 1 == '1' ) выполнится 
    if ( 1 == '1' ) выполнится 
    if ( 2 == '2' ) выполнится 
    if ( 2 == '2' ) выполнится 
    if ( 2 == '02' ) выполнится 
    if ( 2 == '02' ) выполнится 
    if ( 2 == '0x2' ) выполнится 
    if ( 2 == '0x2' ) выполнится 
    // if ( 2 == '0c2' ) не выполнится 
    // if ( 2 == '0c2' ) не выполнится 
    // if ( '-1' == true ) не выполнится 
    if ( '-1' == true ) выполнится 
    // if ( '' ) не выполнится 
    // if ( '' ) не выполнится 
    // if ( NaN == NaN ) не выполнится 
    // 
    - in JS there is even a variable that is not equal to itself! This is NaN


    Have you ever written conditions in JavaScript with complete confidence?
    Some, naively, believe that their insecurity in JavaScript is due to insufficient knowledge of the language. In fact, the root of evil is in Java Script itself. It is specially designed to break your brain. And here is how he does it:

    You, as a thinking person, believe that the == operator compares types. Also, we intuitively feel that zero, null, false and an empty string are something zero. And when casting to a Boolean value, they must give false values. Also, you probably think that the operator ==, if one of the Boolean type arguments first converts the second value to a Boolean type, and then compares it? Ha ha ha, but no, the creators of JavaScript are now rubbing their palms viciously in anticipation of the destruction of your brain cells.

    No, JavaScript certainly leads to a type when compared with the == operator, but it does the opposite of what we expect. Especially those of us who wrote in PHP.

    Here is an excerpt from the documentation for the operator ==:
    If two operands are not of the same type, javascript converts the types and compares strictly. If any operand is a number or a Boolean value, then the operands are converted to numbers; if any operand is a string - the second is converted to a string


    But here is an example:
    
    if( null == false ) // не выполнется никогда
    


    One of the operands is the boolean type. If you believe the documentation, then both operands must be converted to a number. Number (null) will give 0 and Number (false) will give zero. The condition must be satisfied, but this does not happen. Just the creators decided - that null is a special meaning? Feet in my mouth! No, rather he thought like this: null is an Object type. JavaScript false leads to the Object type (it turns out some stupid array object with one false value inside), and then it compares these two objects . Or maybe both of these values
    are cast to a string, get 'null' === 'false'? No, actually:

    If one of the operands is Boolean, then it is converted to 1 (unit), if it is true. And to 0 (zero) if it is false.
    If the object is compared with a number or a string, javascript tries to get the corresponding value for the object. It converts the object to an elementary value, string, or number using the valueOf and toString methods. If the object cannot be converted, a runtime error is generated.


    Anyway, this is not the best solution. We expect the operands to be cast to a bullet, and they are cast to something else. PHP is more expected in this case.

    Next, we check the line with a zero inside for false:
    
    if('0' == false) // да, это условие сработает
    if('0') // но и это условие сработает тоже!
    alert(String(false)); // выведет строку 'false'
    alert(Boolean('0')); // выведет true
    


    Strange, isn't it? If '0' is false, but if we test it directly in the if condition, then it gives true. Everything is simple. The creators of JavaScript decided that if you cast a string to a buoy, the length of this string will be checked . If the string length is greater than zero, this is true. False will only happen if the string is empty. And in the first case, when we check '0' == false, not the string is cast to Boolean, but rather, false is cast to zero and compared with the string '0'.

    Same thing with numbers - even though:
    
    if(true == 2) // не выполнется
    // но
    if(2) // выполнится.
    alert(Boolean(2)); // Выведет true
    alert(Number(true)); // Выведет 1
    


    Why? Everything is simple - Java script when comparing with the == operator will first lead true to unity (1) , and then it will compare it with a deuce (2). Of course they are not equal and the condition will not be fulfilled. As we can see, JavaScript in Boolean operations prefers to cast the bullet to a number, and not vice versa.

    But in vain, there would be fewer incidents.

    Easy exit


    The easiest way is to put double negation before the operands !! . This will unambiguously cast any type to Boolean. But we must remember that !! '0' in Java script will give true, unlike PHP. Because the java script measures the length of the string when casting to Boolean, and PHP first calculates the numeric value of the string, and then leads to the bule.

    Spare the brain of programmers


    If you break your head over the above, then perhaps you will not immediately compose a complete picture of what is happening in JavaScript and why it is different from what is happening in PHP. The picture will be ugly due to incomplete details. The fact is that we like everything that is logical, we like to understand the system,
    all exceptions to the rules are a nuisance. Exceptions undermine understanding, creating a feeling of discomfort. Let’s do understandable things!

    Some writers, diving deep into the program, forget from consumers about those who will use the result of their labors. Forget about what the majority expects. Some internal logic
    justification becomes higher than ease of use, which is fundamentally wrong. Spare the brain of users, take a sober new look at what you are creating, think about what you want at this moment and what you expect from the system. To do this, just listen to yourself. And be sure to ask the opinions of users.

    Otherwise it will turn out as in JavaScript.

    Thank you for your attention, I wish your life was more understandable, Oleg Jozhik.

    PS

    You can see a more visual comparison table here.
    Modest advertising: I recommend using the MicroMail program to send files

    Also popular now: