➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
JavaScript Remove Last Digit from Number | Prior to the advent of Bitwise operators, an integer is converted to a string, and then that string is sliced and the remaining portion is run using string techniques. A type conversion, i.e. converting an integer to a string, is required here.
The debut of Bitwise OR, on the other hand, has made the work a lot easier. Once Bitwise OR is used, there really is no requirement for type conversion and no need for any string operations, which reduces the complexity of the code. We will see both the methods in the article to remove the last digit number in JavaScript.
Example 1:
int = 1029
After removing the last digit from the number:-
102
Example 2:
int = 048
After removing the last digit from the number:-
04
JavaScript Remove Last Digit from Number using substring()
The substring() function displays the portion of the string among the indices provided. To remove the last digit number in JavaScript, use the following code:-
var str = 132401;
var result = str.toString().substring(0, str.toString().length - 1);
console.log(result);
Output:-
13240
JavaScript Remove Last Digit from Number using slice()
The slice() method generates a string that contains the text extracted from a string between the provided indexes. Here’s how to do that with str.slice (0, -n). To remove the last digit number JavaScript we use n, which defines the number of digits or characters that you want to remove. One can delete the last letter or digit from the string with the sample code:
var num = 123456;
var str = num.toString();
num = str.slice(0, str.length - 1);
console.log(num);
Output:-
12345
Let us see another example using a String value.
var str = '683748';
str = str.slice(0, str.length - 1);
console.log(str);
Output:-
68374
Program to remove the last digit from a number using slice() with negative indexes in JavaScript
var str = '985687';
str = str.slice(0, -1);
console.log(str);
Output:-
98568
Remove Last Digit Number JavaScript Using substr()
The substr() method also can be used in JavaScript to remove the last digit from the number. We should make use of the substring() method rather than the substr() method because it is deemed a legacy method.
var str = 132401;
var result = str.toString().substr(0, str.toString().length - 1);
console.log(result);
Output:-
13240
Remove Last Digit from Number Using division method
You could simply divide the value by ten and then use Math.floor() to solve the problem.
var num = 4043498;
var newNum = Math.floor(4043498 / 10);
console.log(newNum)
Output:-
404349
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!