I get a TypeError when fetching and using latest exchange rate

Question:
I am trying to get the latest exchange rate from in in my repl. I then try to display it in my front-end. I get an error in the console TypeError. The network tab has the correct response of the API. I’m not sure what’s happening here.

Current behavior:
TypeError

Desired behavior
Show the latest exchange rate

Repl link:
https://replit.com/@SehjKashyap/CurrencyConverter#script.js

code snippet
function updateExchangeRate() {
  fetch('https://api.exchangerate-api.com/v4/latest/USD')
    .then(response => response.json())
    .then(data => {
      exchangeRate = data.rates.INR;
      document.getElementById('currentExchangeRate').innerText = exchangeRate;
      console.log('Exchange rate updated:', exchangeRate);
    })
    .catch(error => {
      console.error('Error fetching exchange rate:', error);
      document.getElementById('currentExchangeRate').innerText = 'Error fetching rate';
    });
}

You have declared the exchangeRate as a constant

const exchangeRate = 83.85;

And then, here:

You’re trying to reassign a new value to exchangeRate .
You can’t reassign a new value to it if you already declared as a constant.

If you want to update the exchangeRate you have to change from const to let

let exchangeRate = 83.85;
3 Likes