w3resource

JavaScript: parseFloat() function

Description

The parseFloat is used to get a floating value from a string. parseFloat is a top-level function and is not associated with any object.

Syntax

parseFloat(string)

Parameters

string: Required. A string that represents the value to test.

Example of parseFloat() function

The following example demonstrate how to use parseFloat() function.

JavaScript Code:

console.log('"21" -> '+parseFloat("21"));
console.log('"21.42" -> '+parseFloat("21.42"));
console.log('"100 234 54" -> '+parseFloat("100 234 54"));
console.log('"89 Math" ->'+parseFloat("89 Math"));
console.log('"Math 89" ->'+parseFloat("Math 89"));

View the example in the browser

Live demo:

See the Pen parsefloat-function-1 by w3resource (@w3resource) on CodePen.


Previous: JavaScript: parseInt() function
Next: JavaScript: NaN



JavaScript: Tips of the Day

Returns all unique values of an array, based on a provided comparator function

Example:

const tips_uniqueElementsBy = (arr, fn) =>
  arr.reduce((acc, v) => {
    if (!acc.some(x => fn(v, x))) acc.push(v);
    return acc;
  }, []);

console.log(tips_uniqueElementsBy(
  [
    { id: 0, value: 'p' },
    { id: 1, value: 'q' },
    { id: 2, value: 'r' },
    { id: 1, value: 's' },
    { id: 0, value: 't' }
  ],
  (a, b) => a.id == b.id
)); 

Output:

[[object Object] {
  id: 0,
  value: "p"
}, [object Object] {
  id: 1,
  value: "q"
}, [object Object] {
  id: 2,
  value: "r"
}]