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


In this article we are going to discuss how to remove the last digit of a number and execute the remaining digits using JavaScript.

A number firstly should be converted into the string after that you perform your operation like the length of the string  1 after the removed the last element and execute the remaining part. Here type conversion is needed because we are changing the number to a string.

Syntax

Following is the syntax of the substring() method −

str.substring(0, str.length - 1)

This method accepts two parameters as an argument first is the string index from where you want to start and the second is the last index where you want to stop the string value.

But we can also do with bitwise operation bitwise or make the task very simple. You don't need to do type conversion and there is no need to perform any string method. In bitwise or operation first, you need to divide your number by 10 and perform bitwise or operation.

Example: 1

In the following example, we have used the string method to remove the last digit from a number.

<!DOCTYPE html>
<html>
<head>
   <title>removing last digit from number</title>
</head>
<body>
   <script>
      var num1 = 2345;
      let str = num1.toString();
      document.write(str.substring(0, str.length - 1));
   </script>
</body>
</html>

Example 2

In the following example, we have used bitwise or operation to remove the last digit in a number.

<!DOCTYPE html>
<html>
<head>
   <title>removing last digit from number</title>
</head>
<body>
   <script>
      document.write((2345 / 10) | 0);
      document.write("<br />");
      document.write((23456 / 10) | 0);
      document.write("<br />");
      document.write((234567 / 10) | 0);
      document.write("<br />");
      document.write((2345678 / 10) | 0);
      document.write("<br />");
      document.write((23456789 / 10) | 0);
   </script>
</body>
</html>

Example 3

In the following example, we are using array and removing last digit of every array element.

<!DOCTYPE html>
<html>
<head>
   <title>removing last digit from number</title>
</head>
<body>
   <script>
      var array = [23, 45, 56, 67, 78];
      document.write("[")
      for (let i = 0; i < array.length; i++) {
         const element = ((array[i] / 10) | 0);
         document.write(" " + element + " ");
      }
      document.write("]")
   </script>
</body>
</html>

Example 4

Passing an array with some attributes in the _.first() function −

<!DOCTYPE html>
<html>
<head>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
</head>
<body>
   <script type="text/javascript">
      var users = [
         { name: "priya", Long: "false" },
         { name: "aishwarya", Long: "true" },
         { name: "akanksha", Long: "true" },
         { name: "ruby", Long: "true" },
      ];
      document.write(JSON.stringify(_.first(users, 2)));
   </script>
</body>
</html>

Example 5

Passing an arr in _.first() function as a parameter and not passing another parameter, if you are not passing the n value then it will automatically return only the element of the first index or we can say that first element.

<!DOCTYPE html>
<html>
<head>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
</head>
<body>
   <script type="text/javascript">
      var arr = [2, 3, 4, 5, 6];
      document.write(_.first(arr));
   </script>
</body>
</html>

Example 6

We can also find the first n array elements by using an "array. slice()" method.

<!DOCTYPE html>
<html>
<head>
   <title>Slice Method</title>
</head>
<body>
   <script type="text/javascript">
      var arr = [1, 2, 3, 4, 5, 6];
      document.write(arr.slice(0, 4));
   </script>
</body>
</html>

Updated on: 06-Dec-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements