Javascript: check if exist a variable with dynamic name

Issue

This Content is from Stack Overflow. Question asked by Spanu18

I have a problem with my JavaScript code.

I want to check if a variable with dynamic name exist, but I don’t know how to do it.
I tried to do something, but it doesn’t work

If someone could help me, I would be very grateful.
Thx, Alessio

function addElement(id_sub, id_v){
    var n_id_sub = document.getElementById('sub'+id_sub).value;
    var name = "tot" + id_sub;
    checkName(name);
    var n_prod = document.getElementById('number'+id_v).value;
    if(n_prod>=2){
        alert(n_prod)
    }else{
        var inputField=document.getElementById('number'+id_v);
        inputField.value=parseInt(n_prod)+1;
        
    }
}

function checkName(n){
    if(typeof window['name'] === 'undefined'){
        var window['name'] = 0;
    } 
}
    



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?