includes() method not working on strings

2,366 views
Skip to first unread message

Attila Polyak

unread,
Jan 20, 2021, 9:50:36 AM1/20/21
to Google Ads Scripts Forum
hi

I was working on a script that notifies users via email when certain conditions are met. I store all user emails in a string, which I get from a single cell in a spreadsheet, but the error occurres if you simply declare a string variable yourself. The .includes() method seems to not work properly on strings and I'm not sure why.

The following code throws "TypeError: Cannot find function includes in object a...@a.com,b...@bla.bla." error. The variable allEmails is naturally a string, I even checked this with typeof, just to be sure.

var allEmails = "a...@a.com,b...@bla.bla";

  if(allEmails.includes(",")) {
    Logger.log("Multiple emails: " + allEmails.split(","));
  } else {
    Logger.log("One email: " + allEmails);
  }

However, the following script that does practically the same thing works just fine:

  var allEmails = "a...@a.com,b...@bla.bla";

  if(allEmails.indexOf(",")>-1) {
    Logger.log("Multiple emails: " + allEmails.split(","));
  } else {
    Logger.log("One email: " + allEmails);
  }

So yeah... I can get this to work by a rather simple workaround I'm just curious why indexOf() works while includes() doesn't.

Google Ads Scripts Forum Advisor

unread,
Jan 20, 2021, 9:59:47 PM1/20/21
to adwords...@googlegroups.com

Hi Attila,

 

Thanks for reaching out. Unfortunately, not all JavaScript methods are supported in the Google Ads Scripts. Kindly continue to make use of the indexOf method instead in your use case. Let me know if you have other questions.

 

Thanks,

Google Logo
Harry Cliford Rivera
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2B1qV1:ref
Reply all
Reply to author
Forward
0 new messages