I am trying to write a website that gets an number input from the user as TRY (Turkish currency) and converts it to dollar. I want to do this with fetch() in js. Here is my code. If I write console.log(data.tr.rate) instead of rate = data.tr.rate, it prints dollar's current rate which exactly what I want, however when I write the code below it prints "undefined".
var rate;
const m_currency = fetch('http://www.floatrates.com/daily/usd.json').then(res => res.json()).then(function(data){
rate = data.tr.rate;
});
console.log(rate);