Home  >  Article  >  Web Front-end  >  How to convert numbers to string in javascript

How to convert numbers to string in javascript

青灯夜游
青灯夜游Original
2021-10-26 18:00:2823752browse

Javascript method to convert numbers to strings: 1. Add numbers to empty strings, syntax "num """; 2. Use toString() method, syntax "num.toString() "; 3. Use toFixed() method, syntax "num.toFixed()".

How to convert numbers to string in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript converts numbers into strings

1. Use the plus operator

When a number is added to an empty string, JavaScript automatically converts the value to a string.

var n = 123;
n = n + "";
console.log(typeof n); //返回类型为 string

How to convert numbers to string in javascript

2. Use toString() method

var n = 123;
n = n.toString();
console.log(typeof n);

How to convert numbers to string in javascript

3. Use toFixed() method

var n = 123.21;
n = n.toFixed();
console.log(typeof n);

How to convert numbers to string in javascript

[Recommended learning: javascript advanced tutorial

The above is the detailed content of How to convert numbers to string in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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