title: Set headers based on condition in Javascript
body: <p>I am working on a react application, where i am checking for the availability of token in local storage, based on the token existence i need to set the headers.</p>
  
  <p>I have tried by initially initializing the JavaScript object outside the loop and then set the headers in the if else condition.</p>
  
  <pre><code>getAllTopics() {
         const token = localStorage.getItem('authKey');
         var config = {};
  
         if(token){
           const URL = API_URL + `api/get-home-ideas-auth`;
            var config = {
             'Accept' : 'application/json',
             'Authorization' : `Bearer ` + token
         }
  
         } else {
           const URL = API_URL + `api/get-home-ideas`;
              var config = {
             'Accept' : 'application/json'
         }
  
         }
           axios.get(URL, {headers : config})
             .then(res =&gt; {
               if (res.data &amp;&amp; res.data.status === 1) {
                 const topics = res.data.data;
                 console.log(topics);
                 this.setState({ topics: topics, showloader:false});
               } 
  
             })
             .catch(e =&gt; {console.error(e); throw e;});
         }
  </code></pre>
  
  <p>I am getting error <code>Cannot GET /function URL()[nativecode]</code></p>