Why we do not need to define the 'topping' variable?

function takeOrder(topping) { 
  console.log('Order: pizza topped with '+topping+'.');

I wonder why we do not need to define the ‘topping’ variable

Because if we didn’t then the function would be confined to one topping.

1 Like

Thank’s for your response.
But I think my query is a bit different.
I want to know how the data type of ‘topping’ is defined, as we have not declared any separate ‘topping’ variable like var topping = ‘…’;?

Okay, I get it now. topping is declared and defined in the function’s formal parameter (topping). We don’t need to write any special statement. JS recognizes it as a local variable in the function definition.

Local variables are scoped to the function and cannot be seen from the outside. As stated above, it is declared in the function definition, and defined by the caller.


The above is known as the caller (call expression statement) and the string literal, “cheese” will be the definition of the topping variable in the function.

1 Like

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