Value === with function call

I went through ES6, assuming it's easy to upgrade to EcmaScript 2017.

Having walked, I got confused in this code

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

What has the equivalent of ES5

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

I understood the default parameter value from it.

But what does f(1)===50 mean in both codes? What is the use of this?

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 comparing the LHS and RHS of an operator, including the type and not only the values.

But why was it used like that?

Please explain its use.

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

+7
javascript jquery equality function-call triple-equals
source share
5 answers

In my opinion, you almost took it right.

Just put the call to this function along with the triple equal sign in if .

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

Here it is.

A triple equal to a simple comparison operator. And a function call on one side of the triple equal to the operator means the value returned by this function.

Therefore, just treat it like any other comparison operator in javascript.

And please correct me if I misinterpreted your question.

All the best!

+1
source share

This is a rigorous comparative test: the function f(x,y,z) , when called with the parameter value x of 1, returns 50. This would be true if the default parameter values ​​added to the x value are 7 and 42.

These function calls and comparisons are intended solely to provide use cases 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 

- An example of advanced parameter processing. the variable length value ...a is 3, so the test confirms the number of parameters passed to the function after x and y .

+3
source share

f(1)===50 checks if f(1) 50 is equal. If this expression evaluates to true, , then the result of this expression is true . Otherwise, it is false . Since you do not assign this value to a variable as it is, you cannot use it anywhere.

Formally, === is called the strict equality operator. For more information, please see here .

+2
source share

The point here is that its sample code. They show you that a function when called with these arguments is equal to something. The expression itself will do nothing unless you type it into the console.

They could just as easily use commentary.

 f(1, 2, "hello", true, 7) // 9 
+2
source share

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

In your example, if you put all three arguments of a numeric type, you get the number as the result, and then to check whether the result is of the correct type, you should use the === operator.

Perhaps this example will be clearer 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 whether the result is really what you expect from it.

+1
source share

All Articles