0

I came across a piece of Javascript code on the internet that I do not understand. simplified snippet:

assocEmails?.includes(email)

what does the "?" do in this context? I searched around but could not get help on this question.

While searching around I came across another Javascript code snippet (on MDN):

(function() {
  console.log(Array.prototype.includes.call(arguments, 'a'))  // true
  console.log(Array.prototype.includes.call(arguments, 'd'))  // false
})('a','b','c') 

The code works but I do not understand how it works and how I could utilise this kind of code in other situations. It looks like the first bracket defines an anonymous function followed by an immediate call to that function (in the second bracket).

Any pointers to documentation and further explanations would be appreciated.

Tonio
  • 22
  • 4

1 Answers1

0

This ?.operator is modern javascript construct called the optional chaining It is a safe property accessor that won't fail if the accessed object is null or undefined. Last time I've check it was still a proposition and not defintely added to javascript. It seems that it's widely implemented in modern browsers now.

assocEmails?.includes(email) is basically a more concise way of writing
assocEmails && assocEmails.includes(email) or
assocEmails ? assocEmails.includes(email) : assocEmails

The fact of writing a self invoking anonymous function

(function(){
    ....
})()

simply allows isolation of the code within the function, they are blocks of code that will be executed in their own scope, this scope will remain isolated, which can be desirable when you don't want to pollute the parent scope, to prevent accidental collision of variables names from other scripts or preventing a function to be called again by script injection.

(function(){
    var privateVar = ....
    // do something with privateVar
})();
// outside the function
// output true
console.log(typeof privateVar === 'undefined');
remix23
  • 2,632
  • 2
  • 11
  • 21
  • @remixes23... thanks for your answer on "optional chaining", I had just found that in MDN before reading your reply. I still do not understand the "self called function". I will try to do a search on "self called function" now (thanks for naming it for me). Can you provide any references or further sources? Many thanks – Tonio Oct 31 '21 at 12:01
  • Isn't the scope of a variable defined inside a function limited to that function? (function() { console.log(Array.prototype.includes.call(arguments, 'a')) // true console.log(Array.prototype.includes.call(arguments, 'd')) // false })('a','b','c') This code's output is based on the calling parameters... right. Sorry, still cannot see the point. Is it like an anonymous function that calls itself - and only exists in-situ. ??? – Tonio Oct 31 '21 at 12:05
  • This answer was very revealing. https://stackoverflow.com/questions/592396/what-is-the-purpose-of-a-self-executing-function-in-javascript Thanks again remix23. – Tonio Oct 31 '21 at 12:18
  • These self invoking anonymous functions (proper term) are blocks of code that will be executed in their own scope, this scope will remain isolated, which can be desirable when you don't want to polute the parent scope, create accidental collision of variables names or preventing a function to be called again when you want to prevent it. – remix23 Oct 31 '21 at 15:48