简体   繁体   中英

assigning the value of function to a variable of same name in javascript

I was reading about hoisting, and in my editor I was trying this

function loginUser() { return "Hi"}

const loginUser = loginUser();

I am getting the error cannot access "loginUser" before reinitialisation. Has it got something to do with hoisting. could I get the explanation as to why I am getting this error?

I can't reproduce the error you are getting. My JS engines log:

SyntaxError: Identifier 'loginUser' has already been declared

Your function declaration declares a variable named loginUser .

Your const declaration declares a variable name loginUser .

const can't redeclare a variable that already exists in the same scope, hence the error.


Your error message, which seems to be an oddity of your environment, is saying that since you have just said const loginUser you can't read the value loginUser to assign to it because the new one doesn't exist yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM