Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get version number from String in Javascript?

I have a version number with 3 digits as a String,

var version = "1.2.3";

and would like to compare it to another version. To see if version is newer than otherversion,

var otherVersion = "1.2.4";

How would you do it?

like image 896
heffaklump Avatar asked May 11 '10 10:05

heffaklump


1 Answers

Pseudo:

  • Split both on .
  • Compare parts sequentially: Major -> Minor -> Rev (if part exist for both versions).
  • If oV[n] > v[n]: oV is greatest.
  • Else: Compare next subpart.

(See @arhorns answer for a elegant implementation)

like image 103
jensgram Avatar answered Sep 25 '22 23:09

jensgram