Categories
JavaScript Answers

How to get the second digit from a number with JavaScript?

Spread the love

Sometimes, we want to get the second digit from a number with JavaScript.

In this article, we’ll look at how to get the second digit from a number with JavaScript.

How to get the second digit from a number with JavaScript?

To get the second digit from a number with JavaScript, we convert it to a string and destructure it.

For instance, we write

const [, digit] = myVar.toString();

to call myVar.toString to return a string version of the myVar number.

Then we get the 2nd digit with `destructuring.

Conclusion

To get the second digit from a number with JavaScript, we convert it to a string and destructure it.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *