1

I'm Learning Javascript.

What does it mean to have parseFloat & parseInt functions in javascript, when in fact, Javascript does not differentiate between float and integer--there is only the Numeric data type.

Seems like they should have created parseNumber to be in alignment with their data types.

Any comments? Maybe somebody knows the reason. It just seems very odd.

2
  • 2
    "Seems like they should have created parseNumber" It does exist. It is called Number() https://www.w3schools.com/jsref/jsref_number.asp Commented Jun 27, 2019 at 5:04
  • The functions are intended to parse an input string. Only you know what the meaning of the string is: do you want to extract an integer or a decimal number from it? In addition, parseInt has an optional second argument... Commented Jun 27, 2019 at 5:07

4 Answers 4

2

parseInt("1.0") will be same as parseInt("1.1") parseFloat("1.0") will be different to parseFloat("1.1")

Sign up to request clarification or add additional context in comments.

1 Comment

below is also correct with more detail, but this was the first answer (and brief!)
2

These functions are basically to parse string data to the relative format so according to the conditions we can use them

The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

The parseFloat() function parses an argument and returns a floating point number.

console.log(parseInt("1.11"))
console.log(parseFloat("1.11"))

Comments

1

In JS, there exist Number not parseNumber - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number. It works with numeric value, no matter whether it's float or int.

parseInt or parseFloat are generally used to parse string to number.

In decimal number i:e 10.3 the parseInt only return the number before decimal and discard everything after it, whereas with parseFloat it does consider the digits after decimal.

const num = 10.3;

console.log(parseInt(num));
console.log(parseFloat(num));

Comments

1

parseFloat() and parseInt() is javascript functions which is basically use to convert string into Float or Integer number

var a = parseInt("10");
  var b = parseInt("10.00");
  var c = parseInt("10.33");
  var d = parseInt("34 45 66");
  var e = parseInt("   60   ");
  var f = parseInt("40 years");
  var g = parseInt("He was 40");

  var h = parseFloat("10");
  var i = parseFloat("10.00");
  var j = parseFloat("34 45 66");
  var k = parseFloat("10.33");
  var l = parseFloat("   60   ");
  var m = parseFloat("40 years");
  var n = parseFloat("He was 40");

 console.log("a : " + a);
 console.log("b : " + b);
 console.log("c : " + c);
 console.log("d : " + d);
 console.log("e : " + e);
 console.log("f : " + f);
 console.log("g : " + g);
 console.log("h : " + h);
 console.log("i : " + i);
 console.log("j : " + j);
 console.log("k : " + k);
 console.log("l : " + l);
 console.log("m : " + m);
 console.log("n : " + n);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.