js type conversion to string and boolean type

In the last article, we talked about how to convert numeric types. Today, we will summarize the methods of converting strings and Boolean types:

The main methods of converting strings are:

toString();

String();

The specific usage is shown in the following table:

method example return value illustrate
toString():
undefined and null have no toString() method
['1','2','3'].toString() 1,2,3 Convert the elements of the Array to strings. The resulting strings are comma-separated and concatenated.
true.toString() "true" Returns "true" if the Boolean value is true. Otherwise, return "false".
var num = 111;
console.log(num.toString());
"111" Convert number type to string type
String() String() can convert null and undefined to strings, but not to strings.
String(null), returns "null".
String(undefined), returns "undefined".


The main methods of converting Boolean types are:

Boolean():

The specific usage is shown in the following table:

method example return value illustrate
Boolean():
Converts any type of value to a boolean
Boolean(false) false false, "", 0, NaN, null, undefined, and any other values ​​are converted to true.
Boolean("") false
Boolean(0) false
Boolean (NaN) false
Boolean(undefined) false
Boolean("false") true
Convert "false" of type string to false of type Boolean:
var value = "false";
var flag = value ==="false" ? false : true;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324895116&siteId=291194637