JavaScript study notes three: forced type conversion

One, forced type conversion

1. Definition

  • Cast one data type to another data type. Mainly refers to the conversion of other data types into String, Number, Boolean.

2. Convert other data types to String

(1) Method 1: call toString() method

  • Call the toString() method that needs to convert the data type variable. This method does not affect the data type of the original variable.
  • You need to redefine a new variable, and assign the value of the toString() method that is converted to the data type variable to the new variable, then the data type of the new variable is the String type. Or re-assign to a again to refresh the data type of a.

But when the data type of the variable is Null or Undefined, the variable will report an error when calling the toString method.
So method one is suitable for type conversion of String, Number, Boolean.

Syntax: call the yyy() method of xxx: xxx.yyy();

var a=123;//当前a的数据类型是number
var b=a.toString();//调用a的tostring方法去转换类型,定义新变量b,把a转换之后的类型赋值给b
console.log(typeof a);//结果a还是number类型
console.log(typeof b);//结果b是string类型
var a=123;//当前a的数据类型是number
a=a.toString();//调用a的tostring方法去转换类型然后直接赋值给a,刷新a的数据类型
console.log(typeof a);//结果a就是string类型
  1. When we print an object directly on the page, it is actually the return value of the toString() method of the output object.
  2. The toString() method is in the prototype of the object's prototype.
  3. If you want to not output [object object] when outputting an object, you can add a toString() method to the object to determine its return value.
per.toString=function(){
    
    
  return "我是小per";
}

This only modifies the toString method in one instance. If you want all instances to be modified, you want to modify the prototype object.

Person.prototype.toString=function(){
    
    
  return "Person[name="+this.name];
}

(2) Method 2: Call the String() function

  • Call the String() function, and pass the converted data to the function as a parameter.
  • The function also does not affect the function type of the current variable
  • Call the xxx() function: Syntax: xxx(); write in parentheses when you turn to anyone.
var a=123;//当前a的数据类型是number
console.log(typeof a);//结果a还是number类型
a=String(a);//调用String函数转换数据类型赋值给a,刷新了a的数据类型
console.log(typeof a);//结果a变成String类型

When using the String() function to convert the data type,
there is no difference between using the toString() method for Number and Boolean.
For Null and Undefined, the toString() method
will not be called. It will directly convert null to "null" and
undefined directly Convert to "undefined"

(3) Method 3: Arbitrary value type + empty string

When adding any value , first convert the non-string type to the string type , and add any value type to the empty string "" to convert the type of any value to the string String type. This is an implicit type conversion, which is done automatically by the browser, and actually calls the String() function.

            var a=123;//number类型
			a=a+"";//和空串相加
			console.log(a);
			console.log(typeof a);//输出类型为String类型

3. Convert other data types to Number

(1) Method 1: Call the Number() function

  • Call the Number() function, and pass the converted data to the function as a parameter.

  • When using the Number() function for type conversion:
    1. String ====>>Number:

    (1).如果是纯数字的字符串,那么就直接转换成数字
    (2).如果字符串里面有非数字,则转换为NaN
    (3).如果字符串是空的或者都是空格,则转换结果为0
    
var a="123";//当前a的数据类型是string
a=Number(a);//调用Number函数转换数据类型赋值给a,刷新了a的数据类型
console.log(typeof a);//结果a变成Number类型
console.log(a);//结果为123,开始赋值不同,这里结果就会不同NaN/0
  • Boolean ====>>Number:

       (1).如果布尔值为true,那么转换结果是1
       (2).如果布尔值为false,那么转换结果是0
    
var a=true;//当前a的数据类型是Boolean
a=Number(a);//调用Number函数转换数据类型赋值给a,刷新了a的数据类型
console.log(typeof a);//结果a变成Number类型
console.log(a);//a的结果是1
  • Null====>>Number: The result is 0
  • Undefined====>>Number: The result is NaN

(2) Method two: for string

  • This method is specifically used to deal with strings
  • parseInt() function: convert a string into an integer
  • paserFloat() function: convert a string into a floating point number
var a="123px";//a是字符串
a=parseInt(a);//parseInt()函数
console.log(typeof a);//类型是number
console.log(a);//结果是123
var a="123.345px";//a是字符串
a=paserFloat(a);//paserFloat()函数
console.log(typeof a);//类型是number
console.log(a);//结果是123.345

The parseInt() function can take out the valid integer content in the string, and then convert it to Number. The
paserFloat function is similar to parseInt. It can take out the valid decimal content of the string.

  • For non-String types, use the parseInt() function and the paserFloat() function, it will first convert it to the String type and then perform the operation.

(3) Do -0 for any value; *1; /1 operation

When any value is subtracted, multiplied, or divided , the value will be converted to the Number type first . The return value type after the operation is also the Number type. We can use this feature to perform type conversion, and the value is -0; *1; /1 The result obtained remains unchanged, and the value type is converted to the Number type. The principle is the same as the Number() function, and it is easier to use.

            var a="123";
			var b=a-0;//var b=a*1;var b=a/1;减法,乘法,除法操作
			console.log(b);//结果是123
			console.log(typeof b);//数据类型是number

(4) Method 4: Use "+positive sign" for data type

You can use "+ plus sign" for other data types to make them the Number type. The principle is the same as the Number() function

           var a="123";//字符串类型
			var b=+a;//用“+”
			console.log(b);//123
			console.log(typeof b);//number

4. Convert other data types to Boolean

(1) Method 1: Call the Boolean() function

  • Number ====>>Boolean: Except for 0 and NaN, the rest are true.
  • String ====>>Boolean value: Except for the empty string, the rest are true
  • Null and Undefined are both false when converted to boolean values.
  • The object will also be converted to true.

(2) Method 2: Perform a non-operation on non-Boolean values

  • If a value is reversed twice, the value remains unchanged.
  • If a non-boolean value is negated, it will be converted to a boolean value, and then reversed, so we can use this feature to convert another data type to a boolean value type, which can be an arbitrary data type Reverse twice to convert it to a boolean
var a="1";//a是字符串
a=!!a;//两次取反
console.log(a);//true
console.log(typeof a);//boolean

Guess you like

Origin blog.csdn.net/weixin_45636381/article/details/112391852