简体   繁体   中英

console.log and alert returns undefined

This is a basic question about javascript as I am new I would like to try out different things as I used to do in C. So now my Question is when I use

 console.log(alert()) 

I get the result undefined . Do these functions return no values? If they return values what are those and why I can't check them in the console. Any help is appreciated.

Each function in JavaScript returns a value. If you haven't provided an explicit one, it returns undefined . So alert works and its returned value which is undefined is passed to the console.log .

Check this.

 function foo() { } let result = foo(); console.log(result); 

alert always returns undefined , as does console.log .

If you wanted to save the input from the user in a variable, use prompt :

 const str = prompt('Input something'); console.log('str is', str); 

(Or, even better, use a proper modal instead - alert , prompt and confirm are all considered poor practice because they block )

alert(message);

This shows a message and pauses script execution until the user presses “OK”. it means it expect a string to be pass as a parameter which will be displayed in the prompts

ref = > https://javascript.info/alert-prompt-confirm .

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