Can't use variable defined in if/else statement outside

Hi there,
I have never really got the hang of scopes. I really hate it! Here I am inside an async function and I have an if/else statement where I define the variable ‘url’. But the code outside says that ‘url’ is undefined. WHY??? This is probably really simple, so if you could help me, I’d be sooooo grateful!

    const searchterm = message.content.split("!play ")
    yts( searchterm[1], function ( err, r ) {
  if ( err ) throw err
  const videos = r.videos
var url = videos[0].url;
      console.log(url); //outputs the URL I want
} )
    var url = args[1];
    console.log(url); //outputs the URL I want
  console.log(url); //Outputs undefined

Hope you can help

I tried doing

var url;

before any function and changing it to url = videos[0].url; but it didn’t change it

Fixed by just putting the code that need the variable in both the if and else statements - ugly, but it works