Parses a value into a finite number, returning undefined if parsing fails.
undefined
Unlike parseFloat, this is strict: trailing non-numeric characters (e.g. '1.5abc') cause it to return undefined. Whitespace is trimmed.
parseFloat
'1.5abc'
toFloat('1.5') // 1.5toFloat(' -3.2 ') // -3.2toFloat('1.5abc') // undefinedtoFloat('abc') // undefinedtoFloat('') // undefinedtoFloat(NaN) // undefinedtoFloat(2) // 2 Copy
toFloat('1.5') // 1.5toFloat(' -3.2 ') // -3.2toFloat('1.5abc') // undefinedtoFloat('abc') // undefinedtoFloat('') // undefinedtoFloat(NaN) // undefinedtoFloat(2) // 2
Parses a value into a finite number, returning
undefinedif parsing fails.Unlike
parseFloat, this is strict: trailing non-numeric characters (e.g.'1.5abc') cause it to returnundefined. Whitespace is trimmed.