Home > Web Front-end > JS Tutorial > Three ways to convert JS to String

Three ways to convert JS to String

醉折花枝作酒筹
Release: 2023-01-04 09:36:01
Original
16618 people have browsed it

Methods to convert JS to String: 1. Use the "toString()" method, the syntax is "array name.toString()"; 2. Use the "String()" method, the syntax is "String(array name) "; 3. Use the null character, the syntax is "array name" "operator and null character".

Three ways to convert JS to String

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

Convert to string type, there are three ways: variable .toString(); String('value'); Use ' ' to splice a string;

Each method has a scope of use. We can draw the following conclusions from the following code:

Among the basic data types:

  • Only number, boolean type calls toString() for type conversion. There is no toString() method in undefined and null. toString() can only be used on variables, not constants.

  • undefiend and null can be converted into strings through String(). Constants can be converted to strings using String().

  • All types can be converted into strings by concatenating strings.

In an array, you can call the toString() method and concatenate string conversion, but you cannot use String().

1. toString()

var num = 8;
var numString = num.toString();
console.log(numString); 
var result = true; 
var resultString = result.toString(); 
console.log(resultString); 
// toString 中的参数代表进制 
console.log(num.toString(10))
Copy after login

2. String()

var num1 = 8;
var num2 = String(num1);
console.log(num2);
Copy after login

3. Splice an empty character ""

console.log(num1 + "");
Copy after login

[Recommended learning: javascript video tutorial]

The above is the detailed content of Three ways to convert JS to String. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template