# Accessing the digits of a number in JavaScript

Let now we store not a number in quotes in a variable, but just a number. In this case, an attempt to access its individual character will return `undefined`, since such an access only works for strings:

`let test = 12345; alert(test[0]); // shows undefined`

let's convert our number to a string to solve the problem:

`let test = String(12345); // string alert(test[0]); // shows '1' - everything works`

Suppose now we want to find the sum of the first two digits:

`let test = String(12345); // string alert(test[0] + test[1]); // shows '12' - concatenates like strings`

Let's add the function `Number` so that the characters are summed as numbers:

`let test = String(12345); // string alert(Number(test[0]) + Number(test[1])); // shows 3`

I remind you that a problem of this kind will arise only with summation. When multiplying, for example, conversion to numbers can be omitted:

`let test = String(12345); // string alert(test[1] * test[2]); // shows 6`

The number given is `12345`. Find the sum of the digits of this number.

The number given is `12345`. Find the product of the digits of this number.

The number given is `12345`. Rearrange the digits of this number in reverse order.

←→
↑
en ru