JavaScript type conversion from Java introduction to architect tutorial

Posted by sp@rky13 on Sun, 28 Nov 2021 13:35:12 +0100

1, Automatic type conversion

Automatic type conversion is generally associated with the root running environment and operators. It is an implicit conversion. It seems elusive, but it actually has a certain regularity. It can be roughly divided into: conversion to string type, conversion to boolean type and conversion to numeric type.
                                    Figure 1: automatic type conversion sequence

2, Function conversion (String to Number)

JS provides two global conversion functions: parseInt() and parseFloat(). The former converts the value to an integer, and the latter converts the value to a floating point number. Only by calling these methods on String type can these two functions run correctly; NaN(Not a Number) is returned for other types.

1,parseInt()

Before conversion, the string will first be analyzed to determine whether the character at position 0 is a valid number. If not, it will directly return NaN and will not continue. If yes, it will continue until a non character is found.
 parseInt("1234blue"); // returns 1234
 parseInt("22.5"); // returns 22
 parseInt("blue"); // returns NaN

 

 

2,parseFloat()

This method is similar to the parseInt() method in that each character is viewed from position 0 until the first non valid character is found, and then the string before the character is converted to a number. However, for this method, the first decimal point is a valid character. If there are two decimal points, the second decimal point will be regarded as invalid, and the parseFloat() method will convert the string before the decimal point into a number.
parseFloat("1234blue"); // returns 1234.0
parseFloat("22.5"); // returns 22.5
parseFloat("22.34.5"); // returns 22.34
parseFloat("blue"); //returns NaN

 

 

3, Display conversion

Almost every Number object provides a toString() function to convert the content into a string, and the toString() function provided by Number can convert a Number into a string.
Number also provides the toFixed() function, which will convert the number into a string according to the specified number of digits after the decimal point and round it.
// Convert content to string form
var data = 10
console.log(data.toString())

// Converts a number to a string according to the number of digits specified after the decimal point and rounds it
data = 1.4;
console.log(data.toFixed(0));
data = 1.49;
console.log(data.toFixed(1));

            
// Not right null and undefined use
data = null
console.log(data.toString())
data = undefined
console.log(data.toString())

 


 
JS provides construction methods for Number, Boolean and String objects, which are used to cast other types of data. At this time, the whole data is operated, not part.
Number(false)            0
Number(true)             1
Number(undefined)          NaN
Number(null)             0
Number( "5.5 ")         5.5
Number( "56 ")          56
Number( "5.6.7 ")       NaN
Number(new Object())    NaN
Number(100)             100

Boolean("");             // false – empty string
Boolean("hi");             // true – non-empty string
Boolean(100);             // true – non-zero number
Boolean(null);             // false - null
Boolean(0);             // false - zero
Boolean(new Object());     // true – object

 


The last cast method, String(), is the simplest because it converts any value to a string. To perform this cast, you only need to call the toString() method of the value passed in as a parameter, that is, convert 1 to "1", true to "true", false to "false", and so on. The only difference between casting to a string and calling the toString() method is that casting a null or undefined value can generate a string without raising an error:
var s1 = String(null); // "null"
var oNull = null;
var s2 = oNull.toString(); // won't work, causes anerror

 

The simplest way to convert to a string is directly after any data + "". If you want to learn the whole new course of java Architect, please send me a private letter or see the information in Figure 1.
 

Topics: Javascript Front-end