zoukankan      html  css  js  c++  java
  • 强制类型转换---JavaScript第一课!

    强制类型转换

    一、将其他数据类型转换为string

       法一:调用toString()方法

    ​       但null和undefined这两个值没有toString()方法

       法二:调用String()函数

    注: 调用xxx的yyy()方法,就是xxx.yyy();

    ​    调用xxx的yyy函数,就是xxx();

    二、将其他的数据类型转换为Number

      法一:调用Number()函数

        1、字符串转为数字;

          a、如果是纯数字的字符串,则直接将其转换为数字;

    ​      b、如果字符串中有非数字的内容,则转换为NaN;

    ​      c、如果字符串是一个空串或一个全为空格的字符串,则转化为0;

    ​     2、布尔值转为数字;

    ​       a、true转为1;

    ​       b、false转为0;

    ​       3、null转为0;

          ​ 4、undefined转为NaN;

      法二:只针对字符串

          1、parseInt() 把一个字符串转换为一个整数;

          2、parseFloat()把一个字符串转换为一个浮点数;

    三、将其他的数据类型转换为Boolean

      调用Boolean()函数

    ​     1、数字转为布尔;

    ​       除了0和NaN,其余的都是true;

    ​     2、字符串转为布尔;

    ​       除了空串,其余都是true;(空格也是true)

    ​     3、null、undefined、object都会转为false;

    # typeof判断变量的类型

    ​   console.log(typeof a); //'undefined'

    ​   console.log(typeof(true)); //'boolean'

    ​   console.log(typeof '123'); //'string'

    ​   console.log(typeof 123); //'number'

    ​   console.log(typeof NaN); //'number'

      ​ console.log(typeof defined); //'number'

    ​   console.log(typeof null); //'object'

    转发 https://www.cnblogs.com/isremya/p/12432089.html

  • 相关阅读:
    HttpClient后台post 请求webapi
    [SQL Server] 复制数据库任务
    C# js 在页面能执行,放在单独js文件不能执行
    Flink 中的kafka何时commit?
    jar依赖
    AI重要算法
    NonWindowJoin
    Rocket MQ 源码解析
    linear algebra
    Theories of Deep Learning
  • 原文地址:https://www.cnblogs.com/qianduanlaoniao/p/12439405.html
Copyright © 2011-2022 走看看