Javascript: check if exist a variable with dynamic name


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;
    var n_prod = document.getElementById('number'+id_v).value;
        var inputField=document.getElementById('number'+id_v);

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


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?