Do you understand the implicit type conversion of js? Let’s review it together.

 Do you understand js implicit type conversion?

    Maybe the writing is not very clear, the logic is not very strict, and there are some mistakes. I hope you can criticize and correct me, and I will correct it in time. I am also writing this article as a learner, reversely recording the process of my own learning and exploration, not pointing out the country, slamming Fang Qiu, supercilious masters, if the writing is not good, I hope you will forgive me.

        Come and take a look at the following contact questions to see if you can

 

  var str = false + 1

   console.lgo(str);

 

    var demo = false == 1;

    console.log(demo);

        The first two should be able to do it. If you can’t do it; I advise you to go back and review the type conversion.

        str prints 1

        When performing calculations here, false is first converted into a number and then added, and the false conversion digit is 0, so 1 + 0 = 1

        demo prints false

        The test here is the same false conversion digit is 0; 0 != 1 The two == here can only return false or true, so it is false

        The following questions have a certain degree of difficulty; but they are not very difficult. It can still be done if you do it well.

  come see the first one       

  if (typeof(a) && -true + (+undefined) + " ") {

            console.log('基础很好');

        }

        Here I want to talk about a knowledge point, a very small knowledge point; it is an undeclared variable that will not report an error only in typeof, just remember it

        So typeof(a) returns "undefined" for strings

        Converted to a Boolean value is true; undefined here is the undefined of the string, so it is true+undefined converted to a number is nan 

        Then -1 plus NaN is equal to NaN; any number plus NaN is equal to NaN; then NaN is equal to NaN of a string when adding an empty string. Is this "NaN"

        Converted to a Boolean value is true

 

        Tell a point of knowledge

        That is, which types convert boolean values ​​to false; except for the following values;

        undefined 、null 、false、 ""、 0、 NaN 

       Remember this time and don't forget it again, only these few values ​​are flase during boolean type conversion; the rest are all true.


Look at the second question
    

    if (11 + '11' * 2 == 33) {

            console.log('基础很好');

        }

        Then '11' * 2 here; when performing calculations; because 11 is 11 of a string, it will be converted into 11 of a number before the operation is performed. So 33 is a number type so it can be printed

If you can do a question, you have a good grasp of type conversion; come on ヾ(◍°∇°◍)ノ゙

            !!" " + !!"" - !!false || console('可以打印嘛?');

        The above knowledge points are used again here.

         " " This conversion into a boolean value is true! This means negation, right? Then the two negations are themselves;

        then the next ""

        This is a string pair with nothing; so the conversion to boolean is false

        True plus false is converted into a number, whether it is 0 + 1 or 1; and then converted into a Boolean value is true;

         && || has the function of interrupting even && returns when it encounters false

        And || returns when it encounters true and does not execute the following code

        How is it very simple? did you make it

Guess you like

Origin blog.csdn.net/m0_48557659/article/details/117409271