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

js如果强制转化一个非数字的值为number,会得到一个NaN的值

WBOY
Release: 2016-06-01 09:55:04
Original
1642 people have browsed it

我们大家都知道,在js中,只需要在变量值前面加上变量类型,就可以将变量强制转化为对应的类型。如var a="11"; 那么number(a)是将字符串"11"转换为整形11。 但是,当变量为非数字值时,又会出现什么结果呢?

请看实例:

var a="11";

var b=number(a); 变量b的值为整形11.

但是,如果变量值为非数字时,将返回NaN

如:

var c="sd";

var d=number(c); 变量d的值为NaN

 

小结:

通过此实例,我们可以利用NaN的值来判断变量值是否为数字值。

 

 

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!