Some JS Built In Methods

Photo by Christopher Robin Ebbinghaus on Unsplash

parseInt()

parseFloat()

isFinite()

isNaN()

parseInt()

Syntax:

parseInt(string [, radix])

The built-in function parseInt() takes up to two arguments, a string and a radix, it parses through the string and returns with either an integer, or NaN. There are only two instances in which parseInt would return NaN, first when either the radix is smaller than 2 or bigger than 36, or secondly, when the first non-whitespace character is not a number.

When parseInt does not return NaN, the return value will be the integer in the string argument that can be converted into a number in the specified radix.

Example:

parseInt(“10 likes”, 10) // => 10parseInt(“10 likes”, 8) // => 8parseInt(“10 likes”, 1) // => NaN

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and the rest of the characters and returns the integer value parsed up to that point.

There are only two two signs that parseInt understand properly, the “+” for positive and “-“ for negative integers. The function handles this initially right after all the whitespace is removed. If the are no signs, it skips this step and moves on, but if it does then it removes the sign and parses for the integer in the rest of the string.

Example:

parseInt(“-10 likes”, 10) // => -10

If the radix is undefined, or 0 JS has to assume what the number should be, it will just take the first number that is there in the string, not all browsers support this yet, therefore, you should always specify a radix when using parseInt for decimal placed integers.

PARSEFLOAT();

Syntax:

parseFloat(string);

Another built-in function is parseFloat(). The parseFloat function parses the given string argument into a float, or returns NaN if the first non-whitespace character cannot be converted to a number.

If the function parses through a string and stops as soon as the character is not either a “ +, -, integer 0–9, decimal point, or exponent, and ignores any other character. ParseFloat will also stop parsing once it reaches a second decimal point. Any leading or ending whitespace is also ignored. The function can also parse and return Infinity.

Example:

parseFloat(‘1.99’) // => 1.99parseFloat(‘1.00.75’) // => 1parseFloat(‘Infinity’) // => Infinity

isFinite();

Syntax:

isFinite(testValue);

The function isFinite will return false if the given argument or “testValue” is positive or negative Infinity, NaN, or undefined. Otherwise it will always return true. It is a way to find out whether the number you enter is finite or not, it is a very simple function, and can help helpful in many use cases.

isNan();

Syntax:

isNaN(value);

This function is also very simple in which it returns true or false, but it is very useful! The function will return true if the given value is not a number, but if the value is a number it will return false.

isNan is very useful because when trying to compare in JS it is not possible to rely on the equality operators “ == and === “ to determine whether a value is NaN or not because both NaN == NaN will return false, as well as NaN === NaN.

NaN == NaN //=> false
NaN === NaN //=> false

These are just four of the many many built in functions or methods, is JavaScript, that can help you along your way, if you are looking for more resources, check the link below.

Resource: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store