skip to Main Content
let number2=prompt(" specify enter").trim();  // input= "25"
let number1="25";

console.log(Number(number2)) // output= NAN [NOT A NUMBER]
console.log(Number(number1)) // output= 25`

I have already asked chatgpt and chatgpt also agrees with me that if the input you entered is a string "25" , then the output should be same . the output should be :
25
25

2

Answers


  1. You are treating a string like a number.

    you could make sure that your string is a number before performing any Number specific operations on it.

    shortest solution: console.log(+number2)

    The plus sign converts your string to a number.

    Login or Signup to reply.
  2. prompt returns a string

    You get NaN if you enter "25" with quotes since you will be trying to parse ""25""

    let number2 = prompt(" specify enter").trim(); // input= "25" - prompt ONLY returns a string
    let number1="25"; // this is a valid number in a sttring
    console.log(JSON.stringify(number2), Number(number2)) // output= NAN [NOT A NUMBER] because you are trying to parse ""25""
    console.log(JSON.stringify(number1), Number(number1)) // output= 25 because you are parsing "25"
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search