skip to Main Content

my function won’t log all items to the console.

function myfunc(str){
   //return str.split('')[0].match(/[A-Z]/)
   console.log(str)
}
strings = ["hamza", "Hamza", "name", "Name"]
myfunc(...strings)

I’ve tried the spread operator and the apply function but both methods only log the first item. why does it not iterate over each item?

3

Answers


  1. Try this:

    for (let i = 0; i < str.length; i++) {
        console.log(str[i]);
    }
    
    Login or Signup to reply.
  2. I can see you are trying to use the spread operator while calling the function, but in that case, you need to use the rest parameter syntax if you want to catch all the arguments being passed to your function.
    When you use a spread operator like this –

    strings = ["hamza", "Hamza", "name", "Name"]
    myfunc(...strings)
    

    This is what it would do behind the scenes for all the values from the array –

    strings = ["hamza", "Hamza", "name", "Name"]
    myfunc(strings[0], strings[1], strings[2], strings[3]);
    // OR
    myfunc("hamza", "Hamza", "name", "Name");
    

    If you have a fixed and small number of elements in the array you can do this –

    function myfunc(arg1, arg1, arg3, arg4){
       console.log(arg1, arg1, arg3, arg4)
    }
    

    But if the number of arguments is not fixed then you can use the rest operator, the rest operator will catch all arguments being passed to function in an array.

    function myfunc(...str){
       console.log(str)
    }
    strings = ["hamza", "Hamza", "name", "Name"]
    myfunc(...strings)
    

    Rest parameters

    The rest parameter syntax allows a function to accept an indefinite number of arguments as an array, providing a way to represent variadic functions in JavaScript.

    Check this out for more details –

    1. Rest Parameters
    2. Spread Operator
    Login or Signup to reply.
  3. A function specified with a single parameter will always take single parameter. It cannot take more than that. You are spreading the string variable and it is passing the first element of the array to the function since it can only take one parameter.

    If you have specified two parameters then spread operator would pass first two elements of the array.

    If you want to spread the array, then just simply pass it to the function without spreading it. And then spread it inside the console.log(). For example:

    function myfunc(str) {
      console.log(...str);  // Spread here
    }
    
    let string = ["hamza", "Hamza", "name", "Name"];
    myfunc(string);
    

    Output:

    hamza Hamza name Name
    

    Or, if you want to print them in new lines. You could use join() method of Array data type. For example:

    function myfunc(str) {
      str = str.join("n");
      console.log(str);
    }
    
    let string = ["hamza", "Hamza", "name", "Name"];
    myfunc(string);
    

    Output:

    hamza
    Hamza
    name
    Name
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search