how to check if a string contains any value (no matter symbol or any thing) in js?

I am creating a webpage where the user is asked to enter his name, and his name is stored in his browser. If he/she opens the same page they should be greeted with the name they entered while they were asked before. I tried

var x; //x is undefined to check if it contains value 
function name() {
    x = prompt("Input your name");
}

function save() {
    localStorage.setItem('name', x); //saving name in local storage that is in x variable
    let myName = localStorage.getItem('name'); //getting the item name
}
if (x == undefined) { // if x doesn't contains value it ask to enter name 
    name();
} else {
    save(); // if there is 
    document.write(x)
}
document.write(names)

What am I doing incorrectly?

Answer

var name = localStorage.getItem('name');
if (!name) {
  name = prompt("Input your name");
  localStorage.setItem('name', name);
}
document.write(name);