DEV Community

David
David

Posted on

JavaScript Function Irquiry

Hey ya’ll, I’m in a weird spot. I am in the process of entering a full time developer role at work. (Woo! I’m ecstatic about the change!)

I’ve been working with front end tech for a little over a year, for the most part, it’s come naturally. I’ve had some struggles, but my good buddy Google (used as a middle between myself and Stack Overflow posts) has delivered when I’ve asked.

However, this new role is working in Vue, TypeScript, Php and Vanilla Js. I’m constantly in over my head, and have arrived at a point where I desperately need some help gaining a better understanding.

I don’t quite understand the point of Function Arguments in JS, weird right?

I use them occasionally, I make them work, I use arguments in methods, But outside of an if statement of a for-loop, I just don’t get what the heck I’m doing with the argument.

For example, I’m using a weather api to pull in a json object, parsing that object and using the weather key’s value to display, along with a corresponding icon (which I’ve downloaded and placed in the same folder as app.js, and I have an if statement that assigns the proper icon depending on the weather value.

This is where my thinking falls apart! I tried to programmatically choose the icon with a function, and spent the next 30 minutes trying to decide what kind of function would be needed, what the argument would be, and what in the world the argument would even do?

If I’m testing value1 === value 2, does there need to be an argument?

I (think) what I’m asking, how would you explain arguments to someone? Does it get used when testing values? Some functions dont need them, And I honestly couldn’t tell you why.

Top comments (2)

Collapse
 
canderson93 profile image
Carl Anderson

It'll probably be helpful to step away from code here and think of it in terms of spoken language.

In coding, functions are broadly "actions" -- sections of code that do something. In spoken language, this would be a verb.

If you think about verbs, they work with or without an object.

  • Without an object -- "I laugh."
  • With an object -- "I laugh at jokes."

When it comes to functions being verbs, objects are your parameters. Importantly, they change the way the verb works.

function laugh(target) {
    console.log(`HAHAHAHA`);
    if (target) {
        console.log(`That ${target} is very funny`);
    }
}

However, even within a verb taking an object, there are variations. Sometimes a very has to take an object: "I pick" doesn't make sense unless you have an object, like "the apple". Other times, it can't take an object, like "I die".

function pick(target) {
    if (!target) throw new Error("What are you even talking about?");

    console.log(`Now I have a ${target}`);
}

function die() {
    console.log('Et tu?');
}

To ground this in the example you provided about choosing an icon - you can express it like this: I choose the icon for _____

The obvious candidate for a parameter is the weather type we're looking for, and moving the selection code inside of it.

function chooseIcon(type) {
    // There's only two weather conditions, right?
    if (type === 'cloudy') { return 'cloudy.jpg'; }
    else { return 'sunny.jpg' }
}
Collapse
 
devdrake0 profile image
Si

Hi David,

Perhaps this will help you. If not, let me know and we can run through whatever isn't clear.