title: How to check to see if an anchor tag exist or not in javascript
body: <p>i'm trying to assign a variable the link of the anchor tag only if an anchor tag exist and if it doesn't exist then assign something else.</p>
  
  <pre><code>var global;
  document.getElementById("img").onmouseover=function(e){
  var link=e.target.closest("a").getAttribute('href');
  //if link exists then assign the value to global
  if(link){
  
  global=link;
  }
  else{
  //do something else
  }
  </code></pre>
  
  <p>i did this but it's only going to else condition. What should I do to achieve this. 
  Note: Please only use javascript and not jquery </p>
  
  <p>Full code:</p>
  
  <pre><code>    var isHovered = false;
      var global;
      const imgs = document.getElementsByTagName('img');
  
      const map = fn =&gt; x =&gt; Array.prototype.map.call(x, fn);
  
      map(img =&gt; {
        img.addEventListener('mouseover', (e) =&gt; {
           if(e.target.closest("a").getAttribute('href')){
  
           global=e.target.closest("a").getAttribute('href');
           }
           else{
           global=e.target.src;
           }
           hoveredBox();
        });
      img.addEventListener('mouseleave', (e) =&gt; {
         isHovered=false;
        });
  
  
      })(imgs)
  
  
      document.addEventListener('keypress', keyDown);
  
      function hoveredBox() {
        isHovered = true;
      }
  
      function keyDown(event) {
        if (!isHovered) return;
        var key = event.keyCode;
        if(key === 115) {
          alert(global);
        }
  
      }
  </code></pre>