zoukankan      html  css  js  c++  java
  • JavaScript 类型转换

    JavaScript 类型转换

    Number() 转换为数字, String() 转换为字符串, Boolean() 转化为布尔值。

    JavaScript 数据类型

    JavaScript 中有 5 中不同的数据类型:

    string

    number

    boolean

    object

    function

    3 种对象类型:

    Object

    Date

    Array

    2 个不包含任何值的数据类型:

    null

    undefined

    typeof 操作符

    你可以使用 typeof 操作符来查看 JavaScript 变量的数据类型。

    实例

    typeof "John"                 // 返回 string

    typeof 3.14                   // 返回 number

    typeof NaN                    // 返回 number

    typeof false                  // 返回 boolean

    typeof [1,2,3,4]              // 返回 object

    typeof {name:'John', age:34}  // 返回 object

    typeof new Date()             // 返回 object

    typeof function () {}         // 返回 function

    typeof myCar                  // 返回 undefined (if myCar is not declared)

    typeof null                   // 返回 object

    请注意:

    NaN 的数据类型是 number

    数组(Array)的数据类型是 object

    日期(Date)的数据类型为 object

    null 的数据类型是 object

    未定义变量的数据类型为 undefined

    如果对象是 JavaScript Array JavaScript Date ,我们就无法通过 typeof 来判断他们的类型,因为都是 返回 Object

    constructor 属性

    constructor 属性返回所有 JavaScript 变量的构造函数。

    实例

    "John".constructor                 // 返回函数 String()  { [native code] }

    (3.14).constructor                 // 返回函数 Number()  { [native code] }

    false.constructor                  // 返回函数 Boolean() { [native code] }

    [1,2,3,4].constructor              // 返回函数 Array()   { [native code] }

    {name:'John', age:34}.constructor  // 返回函数 Object()  { [native code] }

    new Date().constructor             // 返回函数 Date()    { [native code] }

    function () {}.constructor         // 返回函数 Function(){ [native code] }

    你可以使用 constructor 属性来查看是对象是否为数组 (包含字符串 "Array"):

    实例

    function isArray(myArray) {

        return myArray.constructor.toString().indexOf("Array") > -1;

    }

    你可以使用 constructor 属性来查看是对象是否为日期 (包含字符串 "Date"):

    实例

    function isDate(myDate) {

        return myDate.constructor.toString().indexOf("Date") > -1;

    }

    JavaScript 类型转换

    JavaScript 变量可以转换为新变量或其他数据类型:

    通过使用 JavaScript 函数

    通过 JavaScript 自身自动转换

    将数字转换为字符串

    全局方法 String() 可以将数字转换为字符串。

    该方法可用于任何类型的数字,字母,变量,表达式:

    实例

    String(x)         // 将变量 x 转换为字符串并返回

    String(123)       // 将数字 123 转换为字符串并返回

    String(100 + 23)  // 将数字表达式转换为字符串并返回

    Number 方法 toString() 也是有同样的效果。

    实例

    x.toString()

    (123).toString()

    (100 + 23).toString()

    Number 方法 章节中,你可以找到更多数字转换为字符串的方法:

    方法 描述

    toExponential() 把对象的值转换为指数计数法。

    toFixed() 把数字转换为字符串,结果的小数点后有指定位数的数字。

    toPrecision() 把数字格式化为指定的长度。

    将布尔值转换为字符串

    全局方法 String() 可以将布尔值转换为字符串。

    String(false)        // 返回 "false"

    String(true)         // 返回 "true"

    喜欢这样文章的可以关注我,我会持续更新,你们的关注是我更新的动力!需要更多java学习资料的也可以私信我!

    祝关注我的人都:身体健康,财源广进,福如东海,寿比南山,早生贵子,从不掉发!

  • 相关阅读:
    python socket文件传输实现
    python 进程与线程(理论部分)
    python函数-基础篇
    python变量、注释、程序交互、格式化输入、基本运算符
    python基础数据篇
    python基础之从认识python到python的使用
    判断素数
    辗转相除法
    你了解gets()和scanf()吗
    密码破译
  • 原文地址:https://www.cnblogs.com/heqingxiaohuo/p/12307232.html
Copyright © 2011-2022 走看看