Meaning of === with function call

1.4k views Asked by At

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

5

There are 5 answers

0
Himesh Suthar On BEST ANSWER

According to me, you almost took it in a correct way.

Just put that function call along with the triple equals sign in if condition.

if ( f(1) === 50 ){
    console.log(true);
}
else {
    console.log(false);
}

Thats it.

The triple equal to is simply a comparison operator. And the function call on one side of the triple equal to operator means the value returned from that function.

Hence just treat it as any other comparison operator in javascript.

And please correct me if I have misinterpreted your question.!

All the best!

3
Christos On

The f(1)===50 checks if f(1) is equal to 50. If this expression is evaluated to true, then the result of this expression is true. Otherwise it is false. Since you don't assign this value to a variable, as it is, you can't use it anywhere.

Formally, the === is called strict equality operator. For further info please have a look here.

0
Seb Cooper On

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.

0
Scimonster On

The point here is that its example code. They are showing you that the function's results when called with those arguments is equal to something. The expression itself won't do anything, unless you paste it into a console.

They could have just as easily used a comment.

f(1, 2, "hello", true, 7) // 9
0
Jacek P. On

The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.

In your example if you will put all three arguments of numeric type you will get number as the result and then to check if the result has correct type you have to use === operator.

Maybe this example will be more clear in your case:

f(1,1,1)   // returns 3 - numeric type
f(1,1,"1") // returns "111" - string type
//so now if you will write
f(1,1,1) == "3" // true
f(1,1,1) == 3 // true
f(1,1,1) === "3" // false, because the result is 3 not "3" as string.
f(1,1,1) === 3 // true
f(1,1,"1") == "111" // true
f(1,1,"1") == 111 // true
f(1,1,"1") === "111" // true
f(1,1,"1") === 111 // false, because the result is string "111" not 111 number.

So in your case this === operator is used to double check if the result is really what you expect it to be.