Get the second digit of a number in JavaScript
In this post we will give you information about Get the second digit of a number in JavaScript. Hear we will give you detail about Get the second digit of a number in JavaScriptAnd how to use it also give you demo for it if it is necessary.
we will learn how to get the second of a number in JavaScript.
Getting the second digit of a number
To access the second digit of a number:
Convert the number to a string.
Call the charAt() method on it, by passing the second digit index 1.
It returns the character at that index.
Here is an example:
const id = 13456;const secondDigit = String(id).charAt(1);// converting string back to numberconsole.log(Number(secondDigit));
Output:
3
The charAt() method accepts the character index as an argument and return its value in the string.
Similarly, we can also use the square brackets notation [] in JavaScript to get the second digit of a number.
Here is an example:
const id = 13456;const secondDigit = String(id)[1];// converting string back to numberconsole.log(Number(secondDigit));
Output:
3
Hope this code and post will helped you for implement Get the second digit of a number in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs