Search code examples
javascriptjqueryparseintparsefloat

In jQuery, should i use parseFloat(el.width()) or parseInt(el.width(),10)? which one is more preferred


I always had this question:
When i dont mind the exact floating number

Which one is preferred?

parseFloat

    someValue  = parseFloat(el.outerWidth())+parseFloat(ele2.css("marginRight")),

parseInt

    someValue  = parseInt(el.outerWidth(), 10)+parseInt(ele2.css("marginRight"), 10),

Which method is easier for the JS engine?


Solution

  • It's as broad as it's long really. parseFloat is pointless here because the values will always be integers. I'd rather save on bytes and use the unary operator +:

    someValue  = (+el.outerWidth())+(+ele2.css("marginRight"));