gpt4 book ai didi

javascript - isNaN 如何验证此代码中 Prompt 仅返回字符串的值?

转载 作者:行者123 更新时间:2023-12-02 23:55:32 25 4
gpt4 key购买 nike

据我所知,Prompt 只返回 String 类型的值。那么,下面的代码如何正常工作呢?

var a = prompt('Number or String? Verify now!');

if(!isNaN(a)){
alert('This is number.');
}
else alert('This is string.')

最佳答案

来自https://www.w3schools.com/jsref/jsref_isNaN.asp , 全局 isNaN() 函数,将测试值转换为数字,然后进行测试。

The isNaN() function determines whether a value is an illegal number (Not-a-Number).

This function returns true if the value equates to NaN. Otherwise it returns false.

This function is different from the Number specific Number.isNaN() method.

The global isNaN() function, converts the tested value to a Number, then tests it.

Number.isNaN() does not convert the values to a Number, and will not return true for any value that is not of the type Number.

关于javascript - isNaN 如何验证此代码中 Prompt 仅返回字符串的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55408981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com