What is the best method to convert to an Integer in JavaScript?

Posted by Mathew Byrne on Stack Overflow See other posts from Stack Overflow or by Mathew Byrne
Published on 2008-09-25T03:53:48Z Indexed on 2010/03/15 4:09 UTC
Read the original article Hit count: 258

There are several different methods for converting floating point numbers to Integers in JavaScript. My question is what method gives the best performance, is most compatible, or is considered the best practice?

Here are a few methods that I know of:

var a = 2.5;
window.parseInt(a); // 2
Math.floor(a);      // 2
a | 0;              // 2

I'm sure there are others out there. Suggestions?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about best-practices