typeof, type conversion

One: use typeof() for data types

number, string, object, function, undefined, boolean note that there is a bug is
the result of typeof (null) is the object
Note: Variable not defined will complain, but use typeof () when not being given, prints undefined
addition typeof The result of (typeof(undefined))
Insert picture description here
is because typeof() returns the result of string type, such as "undefined", "number", etc.

Insert picture description here

  <script>
    console.log(typeof(a));//undefined
    console.log(typeof(undefined));//undefined
    console.log(typeof(NaN));//number
    console.log(typeof(null));//object
    var a="123abc"
    console.log(typeof(+a));//number
    console.log(typeof(!!a));//boolean
    console.log(typeof(a+""));//string
    console.log(typeof(1=="1"));//boolean
    console.log(typeof(NaN==NaN));//boolean
    console.log(typeof(NaN==undefined));//boolean
    console.log(typeof("11"+11));//string
    console.log(typeof(1==="1"));//boolean
    console.log(parseInt("123abc"));//123
    var num=123123.3456789;
    console.log(num.toFixed(3));//123123.346
    console.log(typeof(typeof(a)));//string
    </script>

Two: Number converts data to number type

Unconvertible NaN

<script>
        //   var num=Number('123')
        var num = Number(true)
        var num = Number(null)
        var num = Number(undefined)
        console.log(typeof (num) + num);
    </script>

Three: parseInt is converted to integer

 <script>
        //    var demo="132ac";
        var demo = "false"
        var test = parseInt(demo);
        console.log(typeof (test) + ":" + test);
    </script>

Four: other types of conversion

String, toString, Boolean, parseFloat
Note: null and undefined cannot use toString, and when toString is used, it is different from other type conversion methods, which is demo.toString()

var demo = "abc";
var test = demo.toString();

Five: implicit conversion

1.isNaN

First Number() internally, then compare the result with NaN, if it is NaN, return true, if not, return false

    <script>
        console.log(isNaN(123));
         // console.log(Number('abc'));
        console.log(isNaN('abc'));
        // console.log(Number(null));//0
        console.log(isNaN(null));//false
        // console.log(Number(undefined))
        console.log(isNaN(undefined));//true
    </script>

2.++/–

Internal first Number

<script>
        var demo="123"
        // demo++
        demo--
        console.log(demo);
    </script>

3. Positive and negative ±

Internal Number, although some cannot be converted into numbers, the type will also be coerced into a number type

<script>
        var demo=+"abc"
        console.log(typeof(demo)+":"+demo);
    </script>

4. Plus+

String() As long as there is a string + number, it will be converted to a string

 <script>
        var demo=123+"abc"
        console.log(typeof(demo)+":"+demo);
    </script>

5.-*/% subtraction, multiplication and division modulus

Number()

<script>
        var demo=123/"abc"
        console.log(typeof(demo)+":"+demo);
    </script>

Note: String minus string will be converted to number, multiplication and division is also
Insert picture description here

6. Test for null and undefined on the console

Insert picture description here

7. The only one who is not equal to yourself

Insert picture description here

Six: type conversion does not occur ===and `==!

`
Type conversion is prone to errors, so we all use absolute equal and absolute inequality

Insert picture description here

Seven: sample questions

1.

 <script>
  var str=false+1;
  document.write(str)//false不是字符串,false为0  0+1=1
  var demo=false==1;
  document.write(demo);//false
  if(typeof(a)&&-true+(+undefined)+""){
     
     //"undefined"&&-1+"NaN"+""    -1+NaN还是 NaN。空串是字符串,NaN+空串为字符串“NaN"  最后"undefined"和"NaN"都是字符串。&&上为真
    document.write('基础扎实');
  }
  if(11+"11"*2==33){
     
     
    document.write("基础扎实")
  }
  !!" "+!!""-!!false||document.write('不能打印')//双重否定,true+false-false=>1+0-0  1//document.write(),由于1为真,符号为或,所以到1为真就停了,不执行document.write了
  </script>

2.

 <script>
    /*在js中将函数声明表达式转换为函数表达式,只需要在函数声明前面加上 +,-,=,~ 或 ! 等运算符或者()抱起来就行了。*/
    /*“()”在js中有多种含义,在这里是作为强制运算符。
    强制运算符内部必须是一个表达式,也就是说function test(){}会被认为是一个表达式,
    就像匿名函数一样:function(){} 。
    但是稍有不同,这是个带名字的匿名函数(Named function expression),
    这个名字在外部是无法访问的,只能在函数内部访问,所以调用会报错。
    最后提一点,javascript里小括号是没有作用域的。

*/
    
  var x=1;
  if(function f(){
     
     }){
     
     //if这个括号把function变成了表达式,立即执行了,结果是true,加到括号里去,f就找不到了,undefined
    x+=typeof f; 
  }
  console.log(x);
 
  </script>

Guess you like

Origin blog.csdn.net/x1037490413/article/details/108823990
Recommended