How to Get Last Digit of Number in JavaScript

How to Get Last Digit of Number in JavaScript | Prior to the invention of Bitwise operators, a number was first converted to a string, after which a portion of the string was divided, and the remaining portion was then performed. In this case, type conversion – converting a number into a string – is required. However, Bitwise OR’s introduction has made the procedure incredibly simple. When Bitwise OR is used, type conversion and the use of any form of string methods are not required, which reduces the effort and code size.

The type conversion and string methods, however, are no longer relevant due to the introduction of Bitwise OR. The code is really concise because of bitwise OR.

How to Get Last Digit of Number in JavaScript using % Operator

The % operator can be used to get the last digit of an integer number. It finds out the remainder after division. So, if we divide a number by 10 then the remainder is the last digit of the given number.

var num = 987;
var lastDigit = num % 10;
console.log(lastDigit);

var str = 9;
console.log(str % 10);

Output:-

7
9

The limitation with the % operator:- To find out the last digit of a given number we can apply the % operator can be used only for integer numbers. On floating-point numbers, it will produce a different result. In that case slice() method can be useful.

var num = 987.562;
var lastDigit = num % 10;
console.log(lastDigit);

Output:-

7.562000000000012

How to Get Last Digit of Number in JavaScript using slice() Method

You can use the slice method to get the last digit of number in JavaScript. The steps include:

To determine a number’s last digit:-

  1. Call the slice() method on the string after stringifying the integer and supplying -1 as a parameter.
  2. The last character in the string will be returned by the slice method.
  3. To get the last digit, change the string back to a number.
const num1 = 783.74;
const lastDigit1Str = String(num1).slice(-1); 
const lastDigit1Num = Number(lastDigit1Str); 
console.log(lastDigit1Num);

Output:-

4

const num1 = -734953;
const lastDigit1Str = String(num1).slice(-1); 
const lastDigit1Num = Number(lastDigit1Str); 
console.log(lastDigit1Num);

Output:-

3

Both integer and float numbers can be represented in this way.

The number is first turned into a string using the String() object so that we may utilize the String.slice() method on it. JavaScript indexes are zero-based. A string’s initial character is represented by the index 0, and it’s last by str.length – 1.

The start index, or the index at which to start extraction, is the only parameter we gave to the slice method. Giving a negative index of -1 instructs the computer to provide the string’s final character.

Similar to passing a string, this. starting at start index length – 1.

const str = 'Hello World';

const last = str.slice(-1); 
console.log(last);

const last1 = str.slice(str.length - 1); 
console.log(last1);

Output:-

d
d

The last step is to use the Number object to change the string that the slice method returns back into a number.

Example:- In the example that follows, the last digit of a number is removed using the string function “string.substring().”

var str = '987';
console.log((str.substring(0, str.length - 1)));

Output:-

98

var str = '987';
console.log((str.substring(str.length - 1, str.length)));

var str = '9';
console.log((str.substring(str.length - 1, str.length)));

Output:-

7
9

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!

Leave a Comment

Your email address will not be published. Required fields are marked *