JavaScript Check if input is number in JavaScript by comparing with NaN without using isNaN() check input number javascript,check input number jquery,check input

I tried to check if the input entered by a user is a number or not with the code below. It didn't work.

var a = Number(prompt("Enter a number"));
if (a !== NaN) {
    console.log("yes");
}
else {
    console.log("no");
}

I tried printing the value of a when the input wasn't a number and the output was NaN. But still, this code doesn't work. I know I can use isNaN(), but why doesn't my code work? And is there some other way to check if the input is a number without using isNaN()?

Answer:1

try isFinite() method

var a = Number(prompt("Enter a number"));
if (isFinite(a)) {
    console.log("yes");
}
else {
    console.log("no");
}
Answer:2

Unlike all other possible values in JavaScript, it is not possible to rely on the equality operators (== and ===) to determine whether a value is NaN or not, because both NaN == NaN and NaN === NaN evaluate to false. Hence, the necessity of an isNaN function.

MDN Documentation

Answer:3

Try to use the typeof operator to test your input value :

if(typeof a ==="number"){ 
   console.log(a+" is a number"); 
}

Here is the documentation : https://msdn.microsoft.com/en-us/library/259s7zc1(v=vs.94).aspx

UPDATE :

Try also to use this answer of an older post : https://stackoverflow.com/a/9716488/2862130

Answer:4

I often use

    +a !== +a

since NaN != NaN !!

Answer:5

How to replace sequential ampersand symbol leaving single ampersand untouched. Below is the script I tried which replaces every ampersand into double semicolon. <html> <body> <p id="...

How to replace sequential ampersand symbol leaving single ampersand untouched. Below is the script I tried which replaces every ampersand into double semicolon. <html> <body> <p id="...

  1. javascript regex replace all
  2. javascript regex replace group
  3. javascript regex replace $1
  4. javascript regex replace with capture group
  5. javascript regex replace special characters
  6. javascript regex replace string
  7. javascript regex replace example
  8. javascript regex replace tester
  9. javascript regex replace match
  10. javascript regex replace non alphanumeric
  11. javascript regex replace spaces
  12. javascript regex replace all spaces
  13. javascript regex replace all matches
  14. javascript regex replace backreference
  15. javascript regex replace characters
  16. javascript regex replace not working
  17. javascript regex replace callback
  18. javascript regex replace variable
  19. javascript regex replace non numeric
  20. javascript regex replace forward slash

I am trying to convert a csv file to json using csvtojson converter in nodejs.My code is as shown below. I get an output of data as shown below. I am not sure why this is happening and how to prevent ...

I am trying to convert a csv file to json using csvtojson converter in nodejs.My code is as shown below. I get an output of data as shown below. I am not sure why this is happening and how to prevent ...

  1. csv to json conversion using python
  2. csv to json conversion using java
  3. csv to json conversion in java
  4. csv to json conversion in python
  5. csv to json conversion in javascript
  6. csv to json conversion in mule
  7. convert csv to json using python
  8. convert csv to json using c# and asp net
  9. convert csv to json using javascript
  10. convert csv to json using java
  11. convert csv to json using jackson
  12. convert csv to json using pandas
  13. convert csv to json using c#
  14. convert csv to json using node js
  15. convert csv to json using excel
  16. convert csv to json using php
  17. convert csv to json using jq
  18. convert csv to json using bash
  19. convert csv to json using pyspark
  20. convert csv to json using powershell

I am new to code-igniter. In my View I added this code to enable google login in my website. <html lang="en"> <head> <meta name="google-signin-scope" content="profile email"> <...

I am new to code-igniter. In my View I added this code to enable google login in my website. <html lang="en"> <head> <meta name="google-signin-scope" content="profile email"> <...

  1. send data from javascript to python
  2. send data from arduino to raspberry pi
  3. send data from child to parent react
  4. send data from flask to html
  5. send data from activity to fragment
  6. send data from android to iphone
  7. send data from javascript to python flask
  8. send data from react to node
  9. send data from python to html
  10. send data from one arduino to another
  11. send data from s3 to kinesis
  12. send data from arduino to processing
  13. send data from matlab to arduino
  14. send data from server to client
  15. send data from raspberry pi to pc
  16. send data from child to parent angular
  17. send data from python to arduino
  18. send data from iframe to parent
  19. send data from javascript to php
  20. send data from react to express

what is the difference between bootstrap.min.css and bootstrap.min.js ? Why bootstrap.min.js need to be included? <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/...

what is the difference between bootstrap.min.css and bootstrap.min.js ? Why bootstrap.min.js need to be included? <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/...

  1. what difference between bourbon and whiskey
  2. what difference between vegan and vegetarian
  3. what difference between psoriasis and eczema
  4. what difference between flu a and b
  5. what difference between ipad and ipad air
  6. what difference between republic and democracy
  7. what difference between inc and llc
  8. what difference between chromebook and laptop
  9. what difference between md and do
  10. what difference between virus and bacteria
  11. what difference between w2 and w4
  12. what difference between perfume and eau de toilette
  13. what difference between cold and flu
  14. what difference between catholic and christian
  15. what difference between diabetes 1 and 2
  16. what difference between california king and king
  17. what difference between macbook air and pro
  18. what difference between airpod 1 and 2
  19. what difference between medicare and medicaid
  20. what difference between bipolar 1 and 2