-1

I have two questions:

1. I have a JavaScript function code:

var firstOrNull = function (elems){
    return (elems.length > 0 ) ? elems[0] : null;
} 

What does ? and : means in this code?

2. What is the meaning of this code:

var stopEvent = function(event){ event.stopPropagation() }
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Santhosh
  • 9,965
  • 20
  • 103
  • 243

3 Answers3

0

? and : pair indicate ternary operator in Javascript.

(elems.length > 0 ) ? elems[0] : null; line means if elems length is greater than zero then return elems[0] otherwise return null.

Dhanu Gurung
  • 8,480
  • 10
  • 47
  • 60
0

This is called the ternary operators

if(elements.length > 0){
   return elems[0];
} else {
return  null;
}

is equivalent to :

return (elems.length > 0 ) ? elems[0] : null;

ternary operators

earthmover
  • 4,395
  • 10
  • 43
  • 74
0
  1. This is Conditional Operator
  2. stopPropagation method of javascript event. It uses to prevent further propagation of the current event.
Alex
  • 11,115
  • 12
  • 51
  • 64