How to select an element with class and loop through each one in JavaScript?

How to select an element with a class in javascript and loop through each element like in jQuery.

We can select any element of webpage using document.querySelector  and loop through each element having same class with forEach  function. Follow below example to understand it better:


    flag = 0;
    idtochoose = '';
    document.querySelector('.ty-shipping-options__method input').forEach(function(node) {
        console.log(node.id);

        if(node.checked){
            flag = 1;
        }
        if(idtochoose == ''){
            idtochoose = node.id;
        }
    });

    if(flag == 0){
        setTimeout(function(){
            document.getElementById(idtochoose).click();
        },500);
    }