How to remove the last digit of a number and execute the remaining digits in JavaScript?

In JavaScript, you can remove the last digit from a number using different approaches. The most common methods involve string manipulation or mathematical operations like bitwise operations.

When working with numbers, you typically need to convert the number to a string to use string methods, or use mathematical operations to avoid type conversion altogether.

Method 1: Using String substring()

The substring() method extracts characters from a string between two specified indices.

Syntax

str.substring(startIndex, endIndex)

To remove the last digit, use str.substring(0, str.length - 1) where the first parameter is the starting index (0) and the second is the ending index (length - 1).

<!DOCTYPE html>
<html>
<head>
   <title>Remove Last Digit Using substring()</title>
</head>
<body>
   <script>
      var num1 = 2345;
      let str = num1.toString();
      var result = str.substring(0, str.length - 1);
      document.write("Original number: " + num1 + "<br>");
      document.write("After removing last digit: " + result);
   </script>
</body>
</html>
Original number: 2345
After removing last digit: 234

Method 2: Using Bitwise OR Operation

The bitwise OR operation with 0 truncates decimal places. When you divide a number by 10 and apply | 0, it removes the last digit without string conversion.

<!DOCTYPE html>
<html>
<head>
   <title>Remove Last Digit Using Bitwise OR</title>
</head>
<body>
   <script>
      document.write("2345 ? " + ((2345 / 10) | 0) + "<br>");
      document.write("23456 ? " + ((23456 / 10) | 0) + "<br>");
      document.write("234567 ? " + ((234567 / 10) | 0) + "<br>");
      document.write("2345678 ? " + ((2345678 / 10) | 0) + "<br>");
      document.write("23456789 ? " + ((23456789 / 10) | 0));
   </script>
</body>
</html>
2345 ? 234
23456 ? 2345
234567 ? 23456
2345678 ? 234567
23456789 ? 2345678

Method 3: Processing Array Elements

You can apply the same logic to remove the last digit from multiple numbers in an array.

<!DOCTYPE html>
<html>
<head>
   <title>Remove Last Digit from Array Elements</title>
</head>
<body>
   <script>
      var array = [23, 45, 56, 67, 78];
      var results = [];
      
      document.write("Original array: [" + array.join(", ") + "]<br>");
      document.write("After removing last digits: [");
      
      for (let i = 0; i < array.length; i++) {
         const element = ((array[i] / 10) | 0);
         results.push(element);
         document.write(element);
         if (i < array.length - 1) document.write(", ");
      }
      document.write("]");
   </script>
</body>
</html>
Original array: [23, 45, 56, 67, 78]
After removing last digits: [2, 4, 5, 6, 7]

Method 4: Using Math.floor()

An alternative mathematical approach uses Math.floor() to achieve the same result.

<!DOCTYPE html>
<html>
<head>
   <title>Remove Last Digit Using Math.floor()</title>
</head>
<body>
   <script>
      var numbers = [1234, 5678, 9012];
      
      for (let i = 0; i < numbers.length; i++) {
         var original = numbers[i];
         var result = Math.floor(original / 10);
         document.write(original + " ? " + result + "<br>");
      }
   </script>
</body>
</html>
1234 ? 123
5678 ? 567
9012 ? 901

Comparison

Method Type Conversion Performance Readability
substring() Required Slower High
Bitwise OR (| 0) Not required Faster Medium
Math.floor() Not required Fast High

Conclusion

The bitwise OR method (num / 10) | 0 is the most efficient for removing the last digit, while substring() offers better readability. Choose based on your performance needs and code clarity preferences.

Updated on: 2026-03-15T23:18:59+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements