# How to Get the First Digit of an Integer in JavaScript?

In JavaScript, you can get the first digit of an integer in the following ways:

## Using Regular Expression

You can use regular expression in the following way to get the first digit of an integer:

1. Convert integer to string and use `String.prototype.match()` method with the `\d` (or `[0-9]`) pattern to get the first digit;
2. Convert numeric string back to integer;
3. Add back the minus sign if the number was originally negative.

For example, you can implement this like so:

```function firstDigit(num) {
// 1: get first digit using regex pattern
const matches = String(num).match(/\d/);
// 2: convert matched item to integer
const digit = Number(matches);
// 3: add sign back as needed
return (num < 0) ? -digit : digit;
}

console.log(firstDigit(1234)); // 1
console.log(firstDigit(-1234)); // -1
console.log(firstDigit(0)); // 0
console.log(firstDigit(-0)); // 0
```

## Using `Math.trunc()`

The following formula would give you the first digit of an integer:

```quotient = trunc(integer / integerLength - 1)
```

You can implement it in the following steps:

1. Convert integer in absolute form to string and get length of the integer to determine the divisor;
2. Get the integer part of the resulting decimal number (using `Math.trunc()`).

For example, you can implement this like so:

```// ES6+
function firstDigit(num) {
// 1: convert absolute form to string and get length of integer
const len = String(Math.abs(num)).length;
const divisor = 10 ** (len - 1);
// 2: get integer part from result of division
return Math.trunc(num / divisor);
}

console.log(firstDigit(1234)); // 1
console.log(firstDigit(-1234)); // -1
console.log(firstDigit(0)); // 0
console.log(firstDigit(-0)); // 0
```

This works in the following way:

```// num = -1234

// len = 4
// divisor = 10 ^ (4 - 1) = 1000

// quotient = trunc(-1234 / 1000)
// quotient = trunc(-1.234)
// quotient = -1
```

## Using Floored Division

The following formula would give you the first digit of an integer:

```quotient = floor(abs(integer) / integerLength - 1)
```

You can implement it in the following steps:

1. Convert integer to absolute form;
2. Get length of the integer to determine the divisor;
3. Get the integer part of the resulting decimal number (using floored division);
4. Add back the minus sign if the number was originally negative.

For example, you can implement this like so:

```function firstDigit(num) {
// 1: convert to absolute form
const dividend = Math.abs(num);
// 2: get length of integer and determine divisor
const len = String(dividend).length;
const divisor = 10 ** (len - 1);
// 3: get integer part from result of division
const quotient = Math.floor(dividend / divisor);
// 4: add sign back as needed
return (num < 0) ? -quotient : quotient;
}

console.log(firstDigit(1234)); // 1
console.log(firstDigit(-1234)); // -1
console.log(firstDigit(0)); // 0
console.log(firstDigit(-0)); // 0
```

Since the dividend is in absolute form, `Math.floor()` method works (similar to using `Math.trunc()`) for both, positive and negative, integers.

This works in the following way:

```// num = -1234

// dividend = abs(-1234)
// dividend = 1234
// len = 4
// divisor = 10 ^ (4 - 1) = 1000

// quotient = floor(1234 / 1000)
// quotient = floor(1.234)
// quotient = 1

// result = -1
```

## Reducing to Single Digit

You can loop over the number and reduce it in each iteration till only a single digit (i.e. digit less than `10`) is left. This can be done in the following steps:

1. Convert integer to absolute form;
2. Reduce number to single digit:
• If integer is greater than `10`, then keep dividing the number by `10` till a number less than `10` is left, or;
• If integer is less than `10`, then return it as is as it's already a single digit number.
3. Get the integer part of the resulting decimal number;
4. Add back the minus sign if the number was originally negative.

For example, you can implement this like so:

```// ES6+
function firstDigit(num) {
// 1: convert to absolute form
let absNum = Math.abs(num);
// 2: reduce number to single digit
while (absNum >= 10) {
absNum /= 10;
}
// 3: get integer part of fraction
absNum = Math.trunc(absNum);
// 4: add sign back as needed
return (num < 0) ? -absNum : absNum;
}

console.log(firstDigit(1234)); // 1
console.log(firstDigit(-1234)); // -1
console.log(firstDigit(0)); // 0
console.log(firstDigit(-0)); // 0
```

Since the operand is in absolute form, you can also use the `Math.floor()` method to achieve the same (instead of using `Math.trunc()`).

## Converting to String and Retrieving First Character

You can convert the integer to string and get the first digit in the following steps:

1. Convert integer in absolute form to string;
2. Get the first character;
3. Convert the numeric character back to integer;
4. Add back the minus sign if the number was originally negative.

For example, you can implement this like so:

```function firstDigit(num) {
// 1: convert absolute form to string
const numStr = String(Math.abs(num));
// 2: get first character
const firstChar = numStr;
// 3: convert back to integer
const firstDigitUnsigned = Number(firstChar);
// 4: add sign back as needed
return (num < 0) ? -firstDigitUnsigned : firstDigitUnsigned;
}

console.log(firstDigit(1234)); // 1
console.log(firstDigit(-1234)); // -1
console.log(firstDigit(0)); // 0
console.log(firstDigit(-0)); // 0
```

Hope you found this post useful. It was published . Please show your love and support by sharing this post.