Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
I'm comparing two variables in an IF statement but I'm getting wrong results

I tried this code and when running for the first time i clicked start and i got me the result "1 is smaller than 2" whick is right but after changing the first input to 10 and clicking start again, it still showed "10 is smaller than 2". Am I doing something wrong here?

<textarea type="number" id="one">1</textarea>
<textarea type="number" id="two">2</textarea>

<a href="#" onclick="start()">Start</a>

<p id="demo"></p>

<script>
function start() {
    var numberOne = document.getElementById('one').value;
    var numberTwo = document.getElementById('two').value;
    
    if(numberOne>=numberTwo) {
        document.getElementById('demo').innerHTML = numberOne + ' is bigger than ' + numberTwo;
    }
    else {
        document.getElementById('demo').innerHTML = numberOne + ' is smaller than ' + numberTwo;
    }
}
</script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are comparing strings, not numbers.

use parseInt or parseFloat to convert it to a number.

var numberOne = parseFloat(document.getElementById('one').value);
about 4 years ago · Juan Pablo Isaza Report

0

Yes it is expected, input in DOM always string even if the type is number.

parse them to number first before comparing.


const intOne = parseInt(numberOne, 10);
const intTwo = parseInt(numberTwo, 10);


// compare them
if (intOne >= intTwo) {
  // do your thing
}

Edit: Update code by @Sebastian Simon suggestion

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!