What is javascript shortcut for this?

if(pf[i].length > highest){ highest = pf[i].length; } 

What is the most effective way to convey the above?

+5
source share
3 answers
Function

Math.max returns the largest number from zero or more.

 highest = Math.max(pf[i].length, highest) 
+7
source

You can use Math.max to get a maximum of two values, highest and pf[i].length .

 highest = Math.max(highest, pf[i].length); 

Or you can also use the ternary operator.

 highest = pf[i].length > highest ? pf[i].length : highest; // ^^^^^^^^^^^^^^^^^^^^^^ Condition // ^^^^^^^^^^^^ Execute when True // ^^^^^^^ Execute when False 

The value from the ternary operation is returned and set to the highest variable.

+3
source

Try the built-in IF statement (ternary operator):

 highest = pf[i].length > highest ? pf[i].length : highest; 
+1
source

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


All Articles