Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Question about accessing array and arrow function with parameter as an argument to another function

noweare

Active Coder
Hello,
Another JS beginner here.I have a couple of questions.

#1 Wondering how the arrow function accesses the two elements of the array in getUsers() as it is not clear to me.
#2 In function findUser the arrow function that is passed has a argument of users. It is used as an
array variable in getUsers but how does one know that seeing there isn't a declaration or definition of it anywhere.

I did try to directly substitute the arrow function in getUsers() instead of passing it to try to figure out how the whole
thing works but could not clear the errors that resulted.


Code:
function getUsers(callback) {
    setTimeout(() => {
        callback([
            { username: 'john', email: '[email protected]' },
            { username: 'jane', email: '[email protected]' },
        ]);
    }, 1000);
}

function findUser(username, callback1) {
    getUsers((users) => {
        const user = users.find((user) => user.username === username);
        callback1(user);
    });
}

findUser('jane', console.log); //console.log is a function that can take a parmteter
 
Solution
Hello,
Another JS beginner here.I have a couple of questions.

#1 Wondering how the arrow function accesses the two elements of the array in getUsers() as it is not clear to me.
#2 In function findUser the arrow function that is passed has a argument of users. It is used as an
array variable in getUsers but how does one know that seeing there isn't a declaration or definition of it anywhere.

I did try to directly substitute the arrow function in getUsers() instead of passing it to try to figure out how the whole
thing works but could not clear the errors that resulted.


Code:
function getUsers(callback) {
    setTimeout(() => {
        callback([
            { username: 'john', email: '[email protected]' },
            { username...
Hello,
Another JS beginner here.I have a couple of questions.

#1 Wondering how the arrow function accesses the two elements of the array in getUsers() as it is not clear to me.
#2 In function findUser the arrow function that is passed has a argument of users. It is used as an
array variable in getUsers but how does one know that seeing there isn't a declaration or definition of it anywhere.

I did try to directly substitute the arrow function in getUsers() instead of passing it to try to figure out how the whole
thing works but could not clear the errors that resulted.


Code:
function getUsers(callback) {
    setTimeout(() => {
        callback([
            { username: 'john', email: '[email protected]' },
            { username: 'jane', email: '[email protected]' },
        ]);
    }, 1000);
}

function findUser(username, callback1) {
    getUsers((users) => {
        const user = users.find((user) => user.username === username);
        callback1(user);
    });
}

findUser('jane', console.log); //console.log is a function that can take a parmteter

Hello!

1. The arrow function in getUsers() is passed as the callback parameter to the getUsers() function. The setTimeout() inside getUsers() simulates a delay for fetching data, and after the delay, the callback function is executed with the array of users as its argument.

2. In JavaScript, function arguments don’t need explicit declarations beforehand. The users parameter in the arrow function is dynamically created when the callback function is invoked by getUsers().

3. If you directly try substituting the function instead of passing it, you’ll likely get errors because you’re breaking the intended flow of callbacks.
-

Hopefully this helps answer some of your question. Let me know if you need anymore help with the code!
 
Solution
Thank you for your helpful answer. That helps me understand whats going on. Coming from a C background where everything must be defined and declared before using. Also in C functions are not used much as parameters to function. JS is huge compared to C. Alot of real estate to cover : )
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom