How can I access the variable declared inside then function outside?

I want to access the input value given/already present in the input field. I am trying the below solution but I can’t access it outside the then function.

    let name
    cy.visit('/')
    cy.get('[id=username]').type("Kanye")
    cy.get('[id=username]').then((input) => {
      age = input.val()
      cy.log(name)  # Works here
    })
    cy.log(name)    #Does not work here

Can you help me modify this function? Or suggest me any alternative I can use.