
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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>
- Related Articles
- Remove number from array and shift the remaining ones JavaScript
- Digit sum upto a number of digits of a number in JavaScript
- Find last five digits of a given five digit number raised to power five in C++
- Reduce sum of digits recursively down to a one-digit number JavaScript
- Sum of the first and last digit of a number in PL/SQL
- Summing up all the digits of a number until the sum is one digit in JavaScript
- The difference of the digits of a two-digit number is 2. The sum of that two-digit number and the number obtained by interchanging the places of its digits is 132. Find the two-digit number(s).
- One of the two digits of a two-digit number is three times the other digit If you interchange the digits of this two-digit number and add the resulting number to the original number, you get $88$. What is the original number?
- A two-digit number is 4 times the sum of its digits and twice the product of the digits. Find the number.
- The units digit of a two digit number is 3 and seven times the sum of the digits is the number itself. Find the number.
- A two digit number is 4 times the sum of its digits and twice the product of its digits. Find the number.
- Sum of the digits of a two digit number is 9. When we interchange the digits of the two digit number, the resultant number exceeds the original number by 27. Find the number.
- The sum of digits of a two-digit number is 8. If 36 is added to the number then the digits reversed. Find the number.
- Execute digits in even places in a JavaScript array?
- Check if the first and last digit of the smallest number forms a prime in Python
