Get the App
SLTechnology News&Howtos  ›  Development  › 

How does javascript determine whether a string is a number

Shulou Source: shulou.com Published: 2022-06-01 17:08:45 11月11日 Update

Most people do not understand the knowledge points of this article "javascript how to judge whether a string is a number", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "javascript how to judge whether a string is a number" article.

In javascript, you can use the Number () function and the isNaN () function to determine whether a string is a number, the syntax "isNaN (" string ", 10)"; if true is returned, the string is not a number, otherwise it is a number.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript determines whether a string is a number

In the process of converting a string to a number through Number (), if the string contains a non-number, NaN will be returned, refer to the following code

Number ("Hello", 10); / / return NAN Number ("110,10); / / return 110Number (" t2110 ", 10); / / return NAN Number (" 1f10g ", 10); / / return NAN

So you can use isNaN () to determine whether the return value of Number () is NaN to determine whether the string is a number. If true is returned, the string is not a number, otherwise it is a number.

Implementation code:

Function f (a) {if (isNaN (Number (a Mag10) {console.log ("not a number");} else {console.log ("is a number");}}

Test:

F ("hello"); f ("10"); f ("d10jh6"); f ("10jh6")

Note: you cannot judge in the following ways:

Number ("Hello", 10) = = NaN;//return false Number ("110", 10) = = NaN;//return false

Because NaN and itself don't want to wait, this is special, and the way to judge NaN is to use isNaN ().

The above is about the content of this article on "how javascript judges whether a string is a number or not". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Numbers characters strings content code functions articles methods knowledge articles special value most that is tutorials more steps environment computers knowledge points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Information NVidia Huawei vpn