JavaScript Program to Find the Sum of the First and Last Digits of a Number
Vikas
2 min readOct 29, 2026
In JavaScript, we can find the sum of the first and last digits of a number using arithmetic operators, loops and string methods. This is a common programming problem for beginners who want to understand number manipulation in JavaScript.
For example, if the given number is 12345, the first digit is 1 and the last digit is 5. Their sum is 1 + 5 = 6.
In this article, we will learn different methods to find the sum of the first and last digits of a number in JavaScript with examples.
Problem Statement
Given a positive integer n, find the sum of its first and last digits.
Example 1:
Input:
n = 12345
Output:
6
Explanation: The first digit is 1 and the last digit is 5. Therefore, the sum is 1 + 5 = 6.
Example 2:
Input:
n = 9876
Output:
15
Explanation: The first digit is 9 and the last digit is 6.…