Your problem, as you know, is that const must be indexed in the same statement in which it was declared.
This does not mean that the value you assign to the constant must be literal. It can be any valid statement truly - a threefold:
const x = IsSomeValueTrue() ? 1 : 2;
Or maybe just assign it to a variable value?
let y = 1; if(IsSomeValueTrue()) { y = 2; } const x = y;
Of course, you can assign it to the return value of the function:
function getConstantValue() { return 3; } const x = getConstantValue();
Thus, there are many ways to make a value dynamic; you just need to make sure that it is assigned in only one place.
Hecksa
source share