What does this JS expression mean?

What does this JS expression mean? What are we coming back?

return dihy(year) in {353:1, 383:1};

+3
source share
5 answers

This is an instruction returnthat forces the containing function to return a boolean value.

  • It calls a function dihy()with the variable value yearas an argument.
  • It checks whether the return value is either 353or 383(the names of the properties that exist in the object literal). It doesn’t matter what the property matters; it just needs to exist inside the object. (That is, 1it's just an arbitrary value.)
  • If so, the function returns true, otherwise false.

JavaScript , , , :

var foo = {353: 1, 383: 1};

function bar(year) {
    return year in foo;
}

alert(bar(1955)); // false
foo[1955] = 1;
alert(bar(1955)); // true

MDC in.

+3

true, dihy year {353:1, 383:1} false .

:

var result = dihy(year);
return result == 353 || result == 383;
+2

:

dihy(year) in {353:1, 383:1}

dihy(year) Number. 353 383, true, - false.

, , return:

return expression; 

, return true, false.

+1

Returns true or false, depending on the result, which dihy()returns 353 or 383 (true for the two, the other is false).

And that means exactly ... is the result of this function contained in this data collection ...

0
source

There is no reason to use the object, i.e. {353: 1, 383: 1}. In fact, the values ​​of 1 are mixed and can make the uninitiated think that the value of 1 is returned when it is not and is purely arbitrary.

The following is equivalent:

dihy(year) in [353, 383]
0
source

Source: https://habr.com/ru/post/1786504/


All Articles