-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,372
  • 15
  • 66
  • 132
Santhosh
  • 7,522
  • 13
  • 60
  • 145

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.

brg
  • 7,970
  • 9
  • 45
  • 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,269
  • 10
  • 43
  • 73
0
  1. This is Conditional Operator
  2. stopPropagation method of javascript event. It uses to prevent further propagation of the current event.
Pinal
  • 10,775
  • 12
  • 49
  • 62