Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
javascript

How to generate an integer in JavaScript

Akande Olalekan Toheeb

Overview

In JavaScript, there are a number of functions that are used to get integers. One such function is ParseInt(), which can be used to obtain an integer from a string.

In this shot, we’ll learn how to use ParseInt() functions to get an integer.

The parseInt() function

The parseInt() is a JavaScript function that parses a string and returns an integer.

const getInteger = console.log(parseInt("007"));

The code above converts a string, “007” into an interger, 7.

If the first character in the string is a letter, it cannot be converted to an integer, and the function will return NaN or null.

const getInteger = console.log(parseInt("M007"));

The ParseInt() function takes radix as the second argument, which represents the base of string and converts an integer into a decimal. ‘Radix’ can be an integer with the range, 2–36.

const getInteger = console.log(parseInt("31021", 4));

RELATED TAGS

communitycreator
javascript

CONTRIBUTOR

Akande Olalekan Toheeb
RELATED COURSES

View all Courses

Keep Exploring