This article will show you how to get the last digit of a number in JavaScript. Here are some ways to do it with examples to make it simple for readers to understand.
Get the last digit of a number in JavaScript
Before answering the question about how to get the last digit of a number in JavaScript? We need to understand what the last digit here means. For example, the number 12345 has the last digit of 5 because it is the last number from left to right, and when we remove it, it will get a new number of 1234.
Use the “%” operator
Here we use the “%” operator to be able to do division and remainder. We will divide the remainder by 10 to get the last digit.
Code:
var sampleNumber = 12345;
// Get the last digit of sampleNumber
lastDigit = sampleNumber % 10;
return (
<div>
<h2>Get the Last Digit of a Number in JavaScript | LearnShareIT</h2>
<hr />
<h3>
The last digit of {sampleNumber} is {lastDigit}
</h3>
</div>
);
}
Output:
So we can get the last digit of a number in JavaScript with a basic code and render it to the screen.
Use Array.pop() method
In this way, we will use the Array.pop() method to get the last digit of the number. You must know how this function works.
Syntax:
array.pop()
Parameter:
- Array: Array to remove element.
First, we will convert the number to a string and use the string.split() method because this method will return us an array of string characters, so the input value is satisfied with the pop() function.
Code:
var number = 54321;
// Pop the last number of numbers array
var lastDigit = number.toString().split('').pop();
return (
<div>
<h2>Get the Last Digit of a Number in JavaScript | LearnShareIT</h2>
<hr />
<h3>
The last digit of {number} is {lastDigit}
</h3>
</div>
);
}
Output:
After we convert the input number to a string, we will use the split() method to split it into an array of single-digit numbers because we are not passing any value to the function. Finally, we use the pop() method to remove the last element of the array of numbers and return it, rendering it to the screen like the output image above. Hope this is helpful to you.
Summary
To summarize, there are many ways to get the last digit of a number in JavaScript. But in this article, we have shown you how to do it by using “%” operator and String. pop() method. Have a good day!
Maybe you are interested:
- Check if Value is a Negative Number in JavaScript
- Convert Integer to its Character Equivalent in JavaScript
- Get the Decimal Part of a Number in JavaScript

Hi, my name’s Nathaniel Kirk. I’m interested in learning and sharing knowledge about programming languages. My strengths are C, C++, Python, HTML, CSS, Javascript, and Reactjs. If you have difficulty with them, don’t worry. I’m here to support you.
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Python, HTML, CSS, Javascript, Reactjs