Javascript Clarity

Definitive answers to some ambiguous javascript.

Some of the most basic things are confusing because everyone has an opinion on it and describes it in ways that we all may not agree with. Let’s absolutely define the terms so that we don’t have to question what it is every time we come across it.

Arguments vs Parameters

We interchange these two all the time. It’s not really that big of a deal but what’s the difference and is there one? Yes there is a difference. Parameters is just a variable names for arguments. So then the actual values you use will be the arguments.

function (x, y) {
console.log(x + y);
};
function(3, 5); //outputs 8

So in this example we can conclude:

Parameters: x, y

Arguments: 3, 5

In conversation I still interchange the terms all the time and people still know what I’m talking about. It’s not that big of a deal, but when it comes down to making it clear and definitive, I choose to give them both clear definitions.

Statements and Expressions

You should check out the stackoverflow for this one. It’s very confusing and it’s seems different for each programming language. I would like a succinct definition that applies across the board and makes sense. I have concluded that:

expressions include variables, operators and values, and keywords or a combination of them, and functions.

statements consist of expressions but perform a task. All statements are expressions, but not all expressions are statements.

examples:

var a; //expresssion
"this is a sentence"; //expression
[0,1,2,3] //expression
{name: "name", color: "color"} //expression
var a = 1; //both
1 + 5; //both
function (){}; //both

Function Statements vs Function Expressions

So now that we’ve defined statements and expressions. How does that apply to this? This is supposedly a function statement:

function test() {
...
};

vs a function expression:

function() {
...
}

Here’s another function expression:

var obj = {
"huh": function test() {
...
}
}

The “huh” function is a “named” function expression. But isn’t function test() a function statement? Very confusing. So here’s what I’ve concluded about the matter. A function statement in javascript is a declaration of a function given a name with this format:

function <name>() {
...
}

Every other function construct is a function expression.

Closing Thoughts

I find it very hard to think through code when my mind is fighting some ambiguous idea. I like concrete things, i’m not a theorist. I have found that establishing concepts definitively makes coding a lot smoother for me. And even if my definition isn’t completely correct I can easily redefine if I understand why it’s not. I have found that arguing over semantics just make things very confusing.

The big takeaway is to make things CONCRETE in your mind and move forward. Ignore the debates when it’s about who is pronouncing “tomato” correctly.