Yes, you can put the values (not variables) in an array.
const array = [];
array.push(1);
array.push(2);
// ...
const sum = array.reduce((sum, value) => sum + value, 0);
Here is my current code looks like var x = BO.F_Number47.getValue();
Based on that, you may not need an array, you can do the sum directly using the values from getValue:
let sum = 0;
for (let n = startNumber; n <= endNumber; ++n) {
sum += BO["F_Number" + n].getValue();
}
For instance, if you wanted the sum of F_Number1 through F_Number47 (inclusive):
let sum = 0;
for (let n = 1; n <= 47; ++n) {
sum += BO["F_Number" + n].getValue();
}
That uses brackets syntax to access F_Number, F_Number2, etc. See this question's answers.
But you may want to refactor and use an array for F instead:
const BO = {
F: []
};
...and then put the object in F; this question's answers talk about doing that.
In a comment you've written:
The F_Number is the inbuilt filed Ids assigned that application assigns to the form if it is a number field. So in order to sum the total of all the field values, I have to refer to the field ids. Also the Ids are not in the sequential order like the first field Id is F_Number1, then F_Number2, F_Number 5.
If you want to sum up the values of all of the F_Number## fields that BO has, regardless of their names, you can do that in a couple of ways.
If the F_Number fields on BO are enumerable, you can use a for-in loop:
let sum = 0;
for (const name in BO) {
if (name.startsWith("F_Number")) {
sum += BO[name].getValue();
}
}
If they aren't enumerable but they are owned by BO (not inherited), you can get the names from Reflect.ownKeys (ES2015+) and use a for-of loop (ES2015+):
let sum = 0;
for (const name of Reflect.ownKeys(BO)) {
if (name.startsWith("F_Number")) {
sum += BO[name].getValue();
}
}
If BO inherits them, you can use Object.getPrototypeOf to go up the prototype chain and check every level:
let sum = 0;
let obj = BO;
while (obj) {
for (const name of Reflect.ownKeys(obj)) {
if (name.startsWith("F_Number")) {
sum += obj[name].getValue();
}
}
obj = Object.getPrototypeOf(obj);
}
Side note: The overwhelmingly-common convention in JavaScript is that variables and property names start with lowercase letters. Constants are sometimes written in all caps, and constructor function start with a single uppercase character, but not typically the names of objects (BO) or properties on them (F_Number47). You can do what you like in your own code, of course, but following common conventions helps when sharing code with others (on the team, or here on SO, etc.).