How to Get the First and Last Digit of a Number in JavaScript
Extracting the first or last digit from a number is a common task in programming, whether for data validation, checksum calculations, or formatting. There are two primary approaches to this in JavaScript: mathematical operations and string manipulation.
This guide will teach you the modern, standard methods for both approaches. You will learn how to use the modulo operator for a highly performant solution with integers and how to use string conversion for a more flexible solution that works with any number.
Goal 1: Get the First Digit of a Number
For example, we want to extract the first digit from a number like 12345.
Recommended Solution: String Manipulation
The most straightforward and flexible method is to convert the number to a string and access its first character. This works for both integers and floating-point numbers.
function getFirstDigit(num) {
// Convert the number to a string and get the first character
const firstChar = String(num)[0];
// Convert it back to a number
return Number(firstChar);
}
// Example Usage:
console.log(getFirstDigit(12345)); // Output: 1
console.log(getFirstDigit(987.65)); // Output: 9
This is the recommended approach for its simplicity and reliability.
An Alternative: Mathematical Approach (only for positive integers)
For positive integers, you can use a loop with division. This is less readable and more complex but avoids type conversion.
function getFirstDigitMath(num) {
let n = Math.abs(num); // Handle negative numbers
while (n >= 10) {
n = Math.floor(n / 10);
}
return n;
}
console.log(getFirstDigitMath(12345)); // Output: 1
Goal 2: Get the Last Digit of a Number
For example, we want to extract the last digit from a number like 12345.
Solution (Recommended for Integers): The Modulo Operator
For integers, the most performant and direct method is the modulo (%) operator. number % 10 will always give you the remainder of a division by 10, which is the last digit.
function getLastDigit(num) {
// Works for both positive and negative integers
return Math.abs(num % 10);
}
// Example Usage:
console.log(getLastDigit(12345)); // Output: 5
console.log(getLastDigit(-987)); // Output: 7
Solution (for any number, including decimals): String Manipulation
If you need to handle floating-point numbers, the string manipulation approach is the only reliable choice.
function getLastDigit(num) {
const numStr = String(num);
const lastChar = numStr.slice(-1);
return Number(lastChar);
}
// Example Usage:
console.log(getLastDigit(123.45)); // Output: 5
console.log(getLastDigit(987)); // Output: 7
Expanding the Logic: Getting the First or Last N Digits
Both of these core methods can be easily extended to get more than one digit.
How to Get the First N Digits
String manipulation is the best tool for this. Use slice() to extract the first N characters.
function getFirstNDigits(number, n) {
const numStr = String(Math.abs(number));
return Number(numStr.slice(0, n));
}
console.log(getFirstNDigits(123456, 3)); // Output: 123
How to Get the Last N Digits
For integers, the modulo operator is still a great choice. To get the last N digits, you use number % (10^N).
function getLastNDigits(number, n) {
const powerOf10 = Math.pow(10, n);
return Math.abs(number % powerOf10);
}
console.log(getLastNDigits(123456, 3)); // Output: 456
Alternatively, slice(-n) provides a flexible string-based solution that also works for decimals.
Conclusion: Which Method Should You Use?
The best method depends on your specific requirements.
- For getting the first digit, string manipulation (
String(num)[0]) is the simplest and most flexible solution, as it works for all number types. - For getting the last digit of an integer, the modulo operator (
num % 10) is the most performant and direct method. - For getting the last digit of a floating-point number, you must use string manipulation (
String(num).slice(-1)).
By choosing the right tool for your specific numeric type, you can write clean, efficient, and reliable code.