Square brackets surrounding parameter in function definition

Asked
Active3 hr before
Viewed126 times

7 Answers

functionsurroundingsquarebrackets
90%

Be advised, that this also works in reverse. Javascript will allow you to "construct" an array from any object by passing the object with brackets around it, and treating it as an array within the function. In this case the function definition will not have brackets around the param.,LavaWings answer above is correct and an excellent analysis of how array destructors work, which is what you get when you declare your function param with brackets.,What confuses me is the square brackets surrounding the value parameter. I can understand it in a function call, but why in function definition?,This is all very surprising to me, but it appears to be valid javascript, according to the ECMAScript 2017 language specification, the formal parameter in a function declaration can any "binding element", including an array binding.

Then a, b, and c will all be declared variables available within the function scope, and in this case, would be equal to the first three elements of the array. Further - if the array passed as an argument doesn't have at least three elements, then the remaining elements specified in the parameter (a, b, and c) will exist as being declared, but will have the value of undefined:

// Example
function destructureThis([a, b, c]) {
   console.log(a, b, c);
}

var shortArray = [1, 25];
destructureThis(shortArray);

// Prints to console:
// 1 25 undefined

Likewise, if the argument array is larger, additional elements are just ignored, as already noted.

var longerArray = [1, 5, 9, 50, 60];
destructureThis(longerArray);

// Prints to console:
// 1 5 9
load more v
88%

What confuses me is the square brackets surrounding the value parameter. I can understand it in a function call, but why in function definition?,The actual behavior of this feature seems to mean that the argument to the function should be an array, and value will take on the value of the first element in the array.,I came across the following code in the Ember CLI website:,This is all very surprising to me, but it appears to be valid javascript, according to the ECMAScript 2017 language specification, the formal parameter in a function declaration can any “binding element”, including an array binding.

I came across the following code in the Ember CLI website:

export default Ember.Helper.helper(function([value]) {
   return value.toUpperCase();
});
72%

A block in JavaScript is anything within a pair of curly braces. The following are examples of blocks.,So, we can say that var is function-scoped. This means whenever a variable is created with var in a function, it will only exist within the function.,If the variable is created outside of the function, it’ll exist in the outer scope.,let, on the other hand, is block-scoped. This means whenever a variable is created with let, it will only exist within its block.

First of all, we can declare variables with the var keyword. Once declared, this variable can be used anywhere in the current scope.

var me = 'Zell'
console.log(me) // Zell
load more v
65%

Using square bracket notation we can create a function which will accept two arguments:,Let’s write a function which will accept an item name and return a price:,We have seen we can access the values of object properties using dot notation and square bracket notation,We learned how we can dynamically look up values of property by using variables with square bracket notation

Let’s see what it could look like:

const car = {
   make: 'Ford',
   model: 'Fiesta',
   color: 'Red'
};
load more v
75%

When an arrow function takes no parameters, the syntax specifies that we include empty parentheses, followed by the arrow (=>), then followed by the code block for the function surrounded by curly braces:,Note: Parameters are the names used while defining the function. Arguments are the values passed while invoking it. In the example above, num1 and num2 are parameters, 4 and 5 are the arguments.,Invoking a function is a simple matter of using the function’s name, followed by parentheses and any arguments needed.,As we can see, when we leave out the return statement the function simply returns undefined.

Below is an example function:

function mult(num1, num2) {
   return num1 * num2
}
const multNum = mult(4, 5);
console.log(multNum); // Expected output: 20
load more v
40%

When an arrow function has no parameters at all, its parameter list is just an empty set of parentheses.,A function definition is a regular binding where the value of the binding is a function. For example, this code defines square to refer to a function that produces the square of a given number:,There are two more or less natural ways for functions to be introduced into programs.,For example, this version of power makes its second argument optional. If you don’t provide it or pass the value undefined, it will default to two, and the function will behave like square.

A function definition is a regular binding where the value of the binding is a function. For example, this code defines square to refer to a function that produces the square of a given number:

const square = function(x) {
   return x * x;
};

console.log(square(12));
// → 144
load more v
22%

You can also remove the parentheses around the argument if the function receives a single argument:,These are both destructing because the brackets are on the LHS of the assignment.,Because all of these functions are expressions that return values, we can chain them together:,The value of the this keyword inside a regular function depends on how the function was called.

{
   // Block Scope
}

if (true) {
   // Block Scope
}

for (var i = 1; i <= 10; i++) {
   // Block Scope
}

function doSomething() {
   // Function Scope
}
load more v

Other "function-surrounding" queries related to "Square brackets surrounding parameter in function definition"