JavaScript study notes (3): data type conversion

0 Table of contents

  • Usage of typeof () function
  • Display type conversion
  • Implicit type conversion
  • "= = =" Operator and "! = =" Operator

1 Usage of typeof () function

JavaScript has six data types: number, string, boolean, undefined, object, function
typeof (). The role is to identify the data type of a certain data.
Pass the data to be identified as a parameter, and the function will return a data type. Usages are as follows:

var num = 123;
console.log(typeof(num));	//输出结果为number

2 Explicit type conversion

2.1 Number (str): Convert the parameter str (usually a string type number) to the number type, and return the conversion result as the return value.
Usages are as follows:

var str = "123";
console.log(typeof(str));	//输出结果为string
var num = Number(str);
console.log(typeof(num));	//输出结果为number

2.2 The usage of parseInt (mix) to
convert mix to integer Int is
as follows:

var a = 1.23;
var b =parseInt(a);
console.log(b);	//输出结果为1

The function can have two parameters: parseInt (mix, radix), where radix is ​​a base, the role is to use mix as a radix base number, and then convert it to a decimal number
.

var a = "a";
console.log(parseInt(a, 16));	//输出结果为10,意思是将16进制的“a”转换为10进制,结果为10
var b = 10;
console.log(parseInt(b, 8));		//输出结果为8,意思是将8进制的“10”转换为10进制,结果为8

The conversion of the parseInt () function starts from the first array type of the converted value, and ends and returns when it encounters a non-numeric type, such as:

var n = "ab123javascript";
console.log(parseInt(n));	//输出结果为NaN,因为n的第一个数据类型(“ab”)是string,非数字类型,直接结束,没有转换结果,所以返回NaN。
n = "123javascript456";
console.log(parseInt(n));	//输出结果为123,"123"是数字类型,所以转为成功,"javascript"是字符串类型,非数字类型,所以转换结束,后面的"456"也不会被转换

2.3
The usage of parseFloat (str) and parseInt (mix) is similar, the function is to convert the string parameter str to a floating type Float
usage such as:

var a = "1.23abc";
console.log(parseFloat(a));	//输出结果为1.23

2.4 The
usage format of toString (radix) is num.toString(radix)that the function is to convert num to String type
radix parameter is optional. When there is radix parameter, it means that num is converted to radix hexadecimal number, and the conversion result is a string type
usage example :

/*无radix参数的示例*/
var a = "1.23";
var strA = a.toString();
console.log(typeof(strA));	//输出结果为string
/*带radix参数的示例*/
var b = 10;
var strB = b.toString(16);
console.log(strB);			//输出结果为十六进制数:"a"

2.5 The
usage of String (mix) is similar to Number (), the function is to convert mix to string type.
Usage is as follows:

var a = "1.23";
var strA = String(a);
console.log(typeof(strA));	//输出结果为string

2.6 The difference between toString () and String ()
toString () can convert all data types except null and undefined to string type, while String () can convert any data type to string type
toString () plus radix parameter It can perform hexadecimal conversion, and String () cannot. For
details, please refer to another blogger's article: https://www.cnblogs.com/leeke98/p/9754859.html

2.7 The function of Boolean (mix)
is to convert mix to Boolean type, the usage is as follows:

Boolean(1);	//将1转换后为true
Boolean(0);	//将0转换后为false
Boolean(-1);	//将-1转换后为true
Boolean("a");	//将"a"转换后为true

3 Implicit type conversion

3.1 The isNaN () function
isNaN (variable), which first performs implicit type conversion, executes Number (variable), and then judges whether its execution result is of NaN type, and returns true if it is, otherwise returns false. Examples are as follows:

3.2 "+ +" and "--" operators
Before performing arithmetic operations, these two operators will first convert the data to be operated into the Number type, and then perform + + or--operations.
Usages are as follows:

var a = "123";
a++;
cosole.log(a);	//运行结果为124

3.3 The plus operator (+)
first converts all operands to string type, and then performs the string concatenation operation. The data type of the execution result is string.
Example:

var a = 1 + "a";
console.log(a);	//输出结果为:1a

3.4 subtraction, multiplication, division, modulo operator (-, *, /,%)
first operands are converted to type number, then subtract, multiply, divide, modulo calculation, execution result data type to a number of
exemplary :

var a = 3 - "2";
console.log(a);	//输出结果为1

3.5 Logical operators: &&, ||,!
First convert the operands to boolean type through the Boolean () function, and then perform logical operations
3.6 equals (==)
This operator has implicit type conversion, such as:

console.log("1"==1);	//输出结果为true
console.log(1==true);	//输出结果为true

4 "= = =" operator and "! = =" Operator

In Section 3.7, there is an implicit type conversion for the equality operation, so sometimes the result of the operation may not be what we want,
so there are "===" operators and "! ==" operators, which are called Absolute equals and absolute unequals, functionally consistent with "==" and "! =", But absolute equals and absolute unequals will not implicitly convert,
so when comparing whether two data are equal or not equal , Usually use absolute equal and absolute not equal ("= = =" operator and "! = =" Operator)

5 Additional supplement: toFixed () function

num.toFixed (n), the function is to keep num n as a decimal, and observe the rounding rules, and the execution result is string type (there is an implicit type conversion)
Example:

var num = 123.345789;
console.log(num.toFixed(3));	//输出结果为"123.346"
var intNum = 123;
console.log(intNum.toFixed(2));	//输出结果为"123.00"

Guess you like

Origin www.cnblogs.com/xiaowus/p/12716647.html