JavaScript trying to understand arguments in js

function f(message)
{

  console.log(message,arguments);

}

f("H", "e", "l", "l", "o");

It shows: H + ["H", "e", "l", "l", "o"]

Question:

why message only shows H, not ["H", "e", "l", "l", "o"]?

Answer:1

Message is the first parameter you are passing to the function, in this case the "H", arguments it's an array-like object that holds all the parameters you pass in.

Some info:

Answer:2

What's happening here is you're passing 5 arguments into f here. Since you have only one param in the function, only the first argument is assigned to message.

However, the other four, as you know, are still in the arguments pseudo-array.

Answer:3

Your function f accepts only one argument i.e. message. But during the function call you are passing 5 arguments i.e. "H", "e", "l", "l", "o". If you want the whole of this to be available in message then either pass using any one of the following methos:

  1. In single string f("Hello").
  2. Concatenated string f("H" + "e" + "l" + "l" + "o")
  3. or Pass it as an array f(["H", "e", "l", "l", "o"])

The arguments variable is an internal function handling mechanism of javascript i.e. whenever you pass parameters to a function it gets assign to the variables that function has defined in its signature but an array list of those variables is also maintained in the function variable called arguments.

Answer:4

Suppose I want to call some JavaScript function. I should keep the required resource file (the JavaScript file) in the application folder and link. But what are the built-in functions like eval(), ...

Suppose I want to call some JavaScript function. I should keep the required resource file (the JavaScript file) in the application folder and link. But what are the built-in functions like eval(), ...

  1. javascript function loaded
  2. javascript function not loaded
  3. javascript function page loaded
  4. javascript check if function loaded
  5. javascript document loaded function
  6. javascript function after page loaded
  7. javascript function after dom loaded
  8. javascript run function when loaded
  9. javascript wait function loaded

So I have 5 grunt-contrib-watch tasks: sass to compile sass files testConcat to concat tests implementationConcat to concat implementation files for testing templates to precompile handlebars ...

So I have 5 grunt-contrib-watch tasks: sass to compile sass files testConcat to concat tests implementationConcat to concat implementation files for testing templates to precompile handlebars ...

I have these two simple functions that change the body background color <script type="text/javascript"> function lightson(){ document.body.bgColor="#EBEBEB"; } function lightsoff(){ ...

I have these two simple functions that change the body background color <script type="text/javascript"> function lightson(){ document.body.bgColor="#EBEBEB"; } function lightsoff(){ ...

I am currently working on an HTML/JS App using Windows Azure Mobile Service. I have two tables, one storing information about an attraction [attractionTable(attractionId, address,...)] and one keeping ...

I am currently working on an HTML/JS App using Windows Azure Mobile Service. I have two tables, one storing information about an attraction [attractionTable(attractionId, address,...)] and one keeping ...

  1. passing query parameters in url
  2. passing query parameters in url angular 7
  3. passing query parameters in url c#
  4. passing query parameters in url node js
  5. passing query parameters in fetch
  6. passing query parameters in curl
  7. passing query parameters in url angular 8
  8. passing query parameters in osb
  9. passing query parameters in postman
  10. passing query parameters to rest service
  11. passing query parameters in url angular 6
  12. passing query parameters in resttemplate
  13. passing query parameters
  14. passing query parameters in mule 4
  15. passing query parameters in angular 2
  16. passing multiple query parameters to rest service
  17. passing multiple query parameters
  18. passing query string parameters in javascript
  19. passing query string parameters in angular
  20. sql query passing parameters