-2

This appears to be one of those things that is impossible to search for because all of the current search engines refuse to do literal searches. So it would be great to get a definitive answer.

If I have a JavaScript statement like:

    const clientRealIpAddress =
        //get ip from behind a nginx proxy or proxy using nginx's 'x-real-ip header
        socket.request?.headers['x-real-ip']
        //get ip from behind a general proxy
        || socket.request?.headers['x-forwarded-for']?.split(',').shift() //if more thatn one x-fowared-for the left-most is the original client. Others after are successive proxys that passed the request adding to the IP addres list all the way back to the first proxy.
        //get ip from socket.request that returns the reference to the request that originated the underlying engine.io Client
        || socket.request?.connection?.remoteAddress
        // get ip from socket.handshake that is a object that contains handshake details
        || socket.handshake?.address

What does the ?. do?

Julian Knight
  • 4,716
  • 2
  • 29
  • 42
  • [?.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) <--- optional chaining. – jsN00b Apr 07 '22 at 10:30
  • 1
    As a tip: google: `js question mark dot` and you'll find the marked dupe. – 0stone0 Apr 07 '22 at 10:31
  • You can find all operators at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence – jabaa Apr 07 '22 at 10:32
  • I should say that I know the answer but want to get an easier to discover answer. ~~I'll post a definitive answer when SO lets me if someone doesn't beat me to it~~. Also worth noting that you _can_ search for `?.` on the MDN site. Or maybe not since you've closed it immediately. :-( That question does not include the right characters in the question name and does not easily show in a search which is why I created this one! – Julian Knight Apr 07 '22 at 10:39

1 Answers1

-1

The optional chaining operator (?.) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.

Check this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

Gil Fitussi
  • 145
  • 8