getting value from radio button javascript

JavaScript
document.querySelector('input[name="rate"]:checked').value;<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        Get value of selected 
        radio button 
    </title> 
</head> 
  
<body> 
    <p> 
        Select a radio button and click on Submit. 
    </p> 
      
    Gender: 
    <input type="radio" name="gender" value="Male">Male 
    <input type="radio" name="gender" value="Female">Female 
    <input type="radio" name="gender" value="Others">Others 
      
    <br> 
      
    <button type="button" onclick="displayRadioValue()"> 
        Submit 
    </button> 
      
    <br> 
      
    <div id="result"></div> 
      
    <script> 
        function displayRadioValue() { 
            var ele = document.getElementsByName('gender'); 
              
            for(i = 0; i < ele.length; i++) { 
                if(ele[i].checked) 
                document.getElementById("result").innerHTML
                        = "Gender: "+ele[i].value; 
            } 
        } 
    </script> 
</body> 
</html>                     
    var elements = document.getElementsByName('radioButton');
    var checkedButton;
    console.log(elements);
    elements.forEach(e => {
        if (e.checked) {
            //if radio button is checked, set sort style
            checkedButton = e.value;
        }
    });
Source

Also in JavaScript: