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


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

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


#2

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


#3

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 = ‘…’;?


#4

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.

takeOrder("cheese")

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.


#5

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