Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of === with function call

I had been going through the ES6 assuming that it would be easy to switch to EcmaScript 2017.

While going through, I got confused about this code

function f (x, y = 7, z = 42) {
    return x + y + z
}
f(1) === 50

Which has ES5 equivalent

function f (x, y, z) {
    if (y === undefined)
        y = 7;
    if (z === undefined)
        z = 42;
    return x + y + z;
};
f(1) === 50;

I did understand the default parameter thing from it.

But what does f(1)===50 mean in both the codes? Whats the use of it?

Here is another example

function f (x, y, ...a) {
    return (x + y) * a.length
}
f(1, 2, "hello", true, 7) === 9

What does f(1, 2, "hello", true, 7) === 9 mean?

I understand that === for comparison between the LHS and RHS of the operator including the type of both and not just value.

But why have it been used like that??

Kindly explain its usage.

This is the link from where I got this. http://es6-features.org/#RestParameter

like image 274
Tirthraj Rao Avatar asked Dec 30 '16 08:12

Tirthraj Rao


People also ask

What does function call mean?

A function call is an expression that passes control and arguments (if any) to a function and has the form: expression (expression-listopt) where expression is a function name or evaluates to a function address and expression-list is a list of expressions (separated by commas).

What is function call with example?

Example 1: Using call() MethodIn the above example, we have defined a function sum() that returns the sum of two numbers. We have then used the call() method to call sum() as sum. call(this, 5, 3) . Here, by default, the this inside the function is set to the global object.

What does function call mean in JavaScript?

The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

What do === mean in jquery?

=== This is the strict equal operator and only returns a Boolean true if both the operands are equal and of the same type.


1 Answers

This is a strict comparison test whether function f(x,y,z), when called with an x parameter value of 1 returns the value 50. This would be true when the default parameter values added to the value of x are 7 and 42.

These function calls and comparisons are purely to provide usage examples and possibly test examples for the functions they call.

The code

function f (x, y, ...a) {
    return (x + y) * a.length
}
f(1, 2, "hello", true, 7) === 9

is an example of extended parameter handling. the ...a variables length property equates to 3 thus the test is confirming the number of parameters passed to the function after x and y.

like image 159
Seb Cooper Avatar answered Oct 04 '22 20:10

Seb Cooper