node.js - Why does passing '1.0' to a function change it to '1' in javascript -


can explain me why when pass 1.0 function in javascript gets converted 1 , how work around quirk?

var return_me = function(value) {     return value; }  console.log("1.0 returned " + return_me(1.0)); 

in javascript there 6 build in types of values.

  1. string
  2. number
  3. boolean
  4. null , undefined
  5. object
  6. symbol

these mentioned book you don't know js find useful in effort learn javascript.

as result js sees var value of function typeof number , understands 1.0 same 1. (in case 1.0 1.9 returns 1.9 expected).

now if want keep these decimals (even if there 0 digits) pass value string.

console.log("1.0 returned " + return_me("1.0")); 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -