How can I use variables from another function in javascript

后端 未结 2 937
孤街浪徒
孤街浪徒 2021-01-26 06:32

I cant have access to my variables from my function UserInfo all my variables are undefined. How can I have access to my variable and display them in my func

2条回答
  •  深忆病人
    2021-01-26 07:04

    The issue with your code is the scope of the variables. In javascript, all variables declared with let have block scope. And you are redeclaring them inside your UserInfo function, so you should just use the variables you had already declared.

    let UserName;
    let UserAge;
    let UserBirthPlace;
    let UserDream;
    
    
    let UserInfo = function() {
      UserName = prompt("What is your name:");
      UserAge = prompt("How old are you: ");
      UserBirthPlace = prompt("Where were you born: ")
      UserDream = prompt("What is your Greatest Dream: ");
    }
    
    let seeInfoUser = function() {
    
      let UserInformation = ` ${UserName} is  ${UserAge} and he was born in ${UserBirthPlace} and his greatest dream is ${UserDream}`
    
      return UserInformation
    }
    
    
    
    let result = seeInfoUser(UserInfo());
    console.log(result)

提交回复
热议问题