<script> var arr= [-1,0,1,2,3,4,5,6,7,8]; var index= arr.indexOf(9); var result =index<0?1:0; console.log(result); <\script> | Sololearn: Learn to code for FREE!

+2

<script> var arr= [-1,0,1,2,3,4,5,6,7,8]; var index= arr.indexOf(9); var result =index<0?1:0; console.log(result); <\script>

Anyone explain me this code? How it run?

4/3/2019 5:31:12 PM

KHUSHbu

2 Answers

New Answer

+4

indexOf method returns the index (position) of the value passed as its argument. When no such value was found in the array, the indexOf returns -1. The 'index' contains -1 because there is no value 9 in array 'arr'. The 'result' variable value was assigned 1 because the ternary operator yields true when it checks whether 'index' value was less then 0, next the value of 'result' is logged in the JS console. Hth, cmiiw

+3

Ohk Thank you