JavaScript(5) | 类型转换和强制类型转换

251 阅读3分钟

强制类型转换

主要是转换为 String , Number ,Boolean

1. 其他类型 ➡ string

方法1: 使用 toString()方法

number➡ String

方法一

var  a =123 ; //数值类型
str_a = a.toString(); //调用toString() 方法
console.log("a=" + a);
console.log("a的类型:" + typeof(a));
console.log("str_a: " + str_a);
console.log("str_a的类型: " + typeof(str_a));

/// console output:
a=123
a的类型:number
str_a: 123
str_a的类型: string

分析: toString()方法是不会影响到原变量,是有返回值的 因此要想真正改变类型,再嵌套一个变量 (即上述的str_a)

方法2: 调用string() 函数

1、null ➡ string

var  a =null ; //null类型
str_a = String(a); //调用String() 方法
console.log("a=" + a);
console.log("a的类型:" + typeof(a));
console.log("str_a: " + str_a);
console.log("str_a的类型: " + typeof(str_a));

// console output
a=null
a的类型:object
str_a: null
str_a的类型: string

2、undefined ➡ string

var  a =undefined ; //数值类型
str_a = String(a); //调用toString() 方法
console.log("a=" + a);
console.log("a的类型:" + typeof(a));
console.log("str_a: " + str_a);
console.log("str_a的类型: " + typeof(str_a));

// console output
a=undefined
a的类型:undefined
str_a: undefined
str_a的类型: string

对于 Number ,Boolean 还是用 tostring() 方法

2. 其他类型 ➡ number

方法1: 使用Number()函数

var  a ="123 " ;
a= Number(a); //type = number

⚠️ Number() 函数N要大写

例子

💛1、string ➡ number

💁‍♂️对于 xxxx ➡ number,有以下几种情况

  1. 如果是纯数字字符串,可以直接转化为数字

  2. 如果字符串中有非数字,则返回值为NaN

    var  a = "123_45" ; // string 非数字字符串
    num_a = Number(a); //调用Number() 方法
     
    console.log("a=" + a);
    console.log("a的类型:" + typeof(a));
    console.log("num_a: " + num_a);
    console.log("num_a的类型: " + typeof(num_a));
    
    // console output:
    a=123_45
    a的类型:string
    num_a: NaN
    num_a的类型: number
    
  3. 如果字符串中有空格,则返回值为NaN

    var a= "123   456";
    a = Number(a);
    console.log( a );
    
    // console output:
    NaN
    

💛2、boolean ➡ number

如果是布尔型类型转数字,则true为1,false 为0

 var  a = true ; //boolean类型
num_a = Number(a); //调用Number() 方法

console.log("a=" + a);
console.log("a的类型:" + typeof(a));
console.log("num_a: " + num_a);
console.log("num_a的类型: " + typeof(num_a));

// console output:
a=true
a的类型:boolean
num_a: 1
num_a的类型: number

💛2、null ➡ number

null类型 ➡ 数字,为 0

var  a = null;
a = Number(a);
console.log(a ); // 0

💛4、undefined➡ number

undefined类型 ➡ 数字,则为 NaN

var  a = undefined;
a = Number(a);
console.log(a ); // NaN

💛4、string类型特殊方法

parseInt() : 将字符串中有效整数读取出来

var a = "123px";
a = parseInt(a);
console.log("a=" + a); // a=123
console.log("a的类型: " + typeof(a)); // a的类型: number

parseFloat(): 将 ...有效小数读取出来

var a = "1200.3gdgdgpx";
a = parseFloat(a);
console.log("a=" + a); // a=1200.3
console.log("a的类型: " + typeof(a)); // a的类型: number

⚠️ 对于读取整数parseInt() 函数,有几种情况

  1. a = "123px";

    //console output:
    123
    number
    
  2. a = "a123px";

    //console output:
    NaN
    number
    
  3. a = "123a456";

    //console output:
    123
    number
    

总结:

  • 第一位是数字,则读取从数字开头的有效数字部分

  • 第一位不是数字,则为NaN

⚠️ 对于读取整数parseFloat() 函数,有几种情况

  1. a = "123.456";

    //console output:
    123.456
    number
    
  2. a = "123.456.789";

    //console output:
    123.456
    number
    

总结:

  • 如果第一位是数字, 则读取是符合小数的小数部分
  • 如果第一位不是数字则取NaN

如果对于非字符串类型,使用parseInt() 或 parseFloat() 会将其变为字符串String类型,再进行变换 如

var a  = true;
a  = parseInt(a ) ; //相当于 a = parseInt ("true");
console.log( a ); // NaN
console.log( typeof a); // number

(完)