How To Get The Second Digit Of A Number In JavaScript

Get the second Digit of a Number in JavaScript

This article will show you four common ways to get the second digit of a number in JavaScript: Using the toString() method, the charAt() method, converting the number to a string, and creating the function to get the second digit. Start reading the detailed article.

Get the second digit of a number in JavaScript

Using the toString() method

Using the toString() method is the way to get the second digit of a number in JavaScript. This method will convert all into a string and is very easy to use.

Code example:

var myNumber = 876392;
console.log("My number is: ", myNumber);

// Using the toString() method with [1]. the string starts with 0
var secondDigit = myNumber.toString()[1];
console.log("The second digit of myNumber is:", secondDigit);

Output

My number is: 876392
The second digit of myNumber is: 7

Converting the number to a string

The second way is converting the number into a string. We use the ‘ ‘ + myNumber to convert the number into the string. And then, we get the second digit by using [1]. The index of the string will start with 0.

Example:

var myNumber = 876392;
console.log("My number is: ", myNumber);

// Using the '' + myNumber to convert a number into a string and get the second digit
var secondDigit = ('' + myNumber)[1];
console.log("The second digit of myNumber is:", secondDigit);

Output

My number is: 876392
The second digit of myNumber is: 7

Creating the function to get the second digit

The next way is creating the function to get another digit of the number. It is easy to take the second digit by using the while loop in JavaScript.

Example:

var myNumber = 876392;
console.log("My number is: ", myNumber);

// Creating the function to get the second digit
function getSecondDigit(number, n) {
	var num = number,
		digits = 0;
	do {
		num /= 10;
		digits++;
	} while (num >= 1) {
		num = number / Math.pow(10, (digits - n));
		num -= num % 1;
		return (num % 10);
	}
}

console.log("The second digit of myNumber is:", getSecondDigit(myNumber, 2));

Output

My number is: 876392
The second digit of myNumber is: 7

Using the charAt() method

The last way is using the charAt() method. This method returns the character at a specified index (position) in a string.

Example:

var myNumber = 876392;
console.log("My number is: ", myNumber);

// Using the charAt() method to return an index 1 of the number
var secondDigit = String(myNumber).charAt(1);
console.log("The second digit of myNumber is:", secondDigit);

Output

My number is: 876392
The second digit of myNumber is: 7

Summary

Congratulation, from now, you can know how to get the second digit of a number in JavaScript in many different ways, but we prefer using the toString() method as the best solution. It is easy to use and helps keep your code clean.

What about your opinion? Leave your comment here to share with us. Thank you for your reading!

Maybe you are interested:

Leave a Reply

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