Home > Web Front-end > JS Tutorial > body text

How to convert numbers to string in javascript

青灯夜游
Release: 2021-10-26 18:00:28
Original
23763 people have browsed it

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
Copy after login

How to convert numbers to string in javascript

2. Use toString() method

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

How to convert numbers to string in javascript

3. Use toFixed() method

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

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template