A beginners question on "call to prompt" and "returns"

The call to prompt returns the text from the input field or null if the input was canceled.

confused…

“call to prompt” and “returns” definition and general clarification (for this example), please

let age = prompt('How old are you?', 100);


alert(You are ${age} years old!);

The prompt(...) part is a call to the prompt function. More specifically, a variable called prompt is not defined, so a higher scope is checked for a prompt variable. When it’s not found then the highest scope in a web browser is the window object, on which is a property called prompt. It is that prompt property that has a function, (properties that are functions are called methods), so it is that prompt method that is called.

When that prompt is complete (either by you selecting OK or Cancel) the return value from that prompt function is what you typed in, or null.

The return value from that prompt, in your code is then assigned to the age variable.

Is that the type of further info that you are after?

Just to add to Paul’s detailed breakdown with something more general — but still relative.

call to prompt

It just means run or start the prompt function — that function being a piece of built in code.

The term has been around for years. Back in the 80’s you might have run a machine code program starting at memory address 8000 with call 8000

returns

In most cases you want something back from a function. For example

function add(x, y) {
  const total = x + y;

  return total; // send the total back
}

const answer = add(6, 4);

alert('6 + 4 is ' + answer) // 6 + 4 is 10

In the above we call the add function, it returns the total and it is assigned or given to the label answer.

To clarify you call a function in Javascript using parentheses after the name e.g. runme().

first, i thank you! you have helped me!

[quote=“rpg_digital, post:3, topic:390127”]

call to prompt

OK!

is this “calling” the the add function?

function add(x, y)

so sorry, the return concept is quite tricky for me!


 In the above we call the add function (see my words above ), it returns 
the total and it is assigned or given to the label answer

lost :frowning: maybe clarify or re-explain?

got it!

No worries, and sorry if I didn’t make it clear.

I did actually do a full breakdown here, but I have decided to delete it and give you this link instead. I think it will be much easier to follow :slight_smile:

If you have any further questions, then do ask.

thank you so much! a lot to learn… but i am determined to get this!

below with function.js


function greet (){

    console.log ("hello there");

}
greet();

beginners question… cant seem to see it in my chrome browser… run the code using VS Code

the man in the YouTube had it quick!

HA!

i figured it out with out you!

i simply look in chrome dev tools and its “logged” in the console

1 Like
    <script>

    
    function greet (){

        console.log ("hello there");
    
    }
    greet();
    greet();
    greet();

</script>

why wont greet(); log 3 times? its in the YouTube

Well done @OBXjuggler, glad you figured it out. (I’m on UK time btw)

why wont greet(); log 3 times? its in the YouTube

I don’t know why your console is doing that.

If you copy your code, no script tags just the following including the three greets.

function greet () {
  console.log('hello there');
}

greet();
greet();
greet();

Go to the chrome console where you see the arrow prompt > paste your code and hit return. Do you see the following output?

hello-there

Some web browsers prevent against multiple of the same message, by showing a number to the left of the same message. That way when a message happens 700 times, you only see 700 to the left of the message, instead of 700 lines of the same identical message.

There is an option in chrome settings for console to set to Group similar messages in console, but even with that set I see 3 hello there's.

When I open the test page and open the browser console, the messages are grouped together.
image

After that when reloading the page, the messages are separate.
image

Whichever way they look, they both mean the same thing.

2 Likes

As you say Paul, it doesn’t really matter.

Just for @OBXJuggler’s info, I did run the example as per NetNinja’s setup with VSCode and Liveview. On mine, running it this way I did see multiple greetings.

If you haven’t already done so, do check out his first video where he sets up vscode and the liveview extention. Starts around the 4 minute mark.

first video here

AH HA!

YES I DO!

BINGO! happened to me that way

that is exactly what happened for me!

crisis averted!

do I owe you all a $0.25 now?

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.