

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove the last digit of a number and execute the remaining digits in JavaScript?
Before the introduction of Bitwise operators, a number is first converted into a string and later on, using string methods, some part of that number is sliced and the remaining part is executed. Here type-conversion i.e a number into a string is necessary. But the introduction of Bitwise or has made the task very easy. When Bitwise or is used there is no necessity of type-conversion and there is no need of using any kind of string methods, reducing the effort and length of the code.
Example
In the following example, a string method called "string.substring()" is used to remove the last digit of a number.
<html> <body> <script> var str = '2345'; document.write((str.substring(0, str.length - 1))); </script> </body> </html>
Output
234
But after the advent of Bitwise or, the type conversion and string methods are nowhere in the picture. Bitwise or has made the code very concise.
Example
<html> <body> <script> document.write(2345 / 10 | 0) document.write("</br>"); document.write(2345 / 100 | 0) document.write("</br>"); document.write(2345 / 1000 | 0) </script> </body> </html>
Output
234 23 2
- Related Questions & Answers
- Remove number from array and shift the remaining ones JavaScript
- Digit sum upto a number of digits of a number in 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
- Reduce sum of digits recursively down to a one-digit number JavaScript
- Find last five digits of a given five digit number raised to power five in C++
- Check if the first and last digit of the smallest number forms a prime in Python
- Find the sum of first and last digit for a number using C language
- Is the digit divisible by the previous digit of the number in JavaScript
- Finding difference of greatest and the smallest digit in a number - JavaScript
- Count of Numbers in Range where first digit is equal to last digit of the number in C++
- Recursive sum all the digits of a number JavaScript
- C program to find sum of digits of a five digit number
- Destructively Sum all the digits of a number in JavaScript
- How to split last n digits of each value in the array with JavaScript?
Advertisements