Ein2012
Ein2012

Reputation: 1103

Is String.contains a standard function in JavaScript?

Recently I was writing logic in JavaScript and I wrote something like this

var str="hello world";

if(str.contains("w"))
    //do something
else
   //do anotherthing

I thought it was working fine until I ran the page in Chrome. In Chrome I'm getting an error of contains is not a function.

Although I got rid of this by modifying the logic as

var str="hello world";
if(str.indexOf("w")!=-1)
    //do something
else
   //do another thing

is contains not a standard ECMAScript function? I'm able to see contains through intellisense in Firefox but not in Chrome.

While testing these in different browsers I noticed in the console that

String.subString/indexOf //not showing in chrome but works in Firefox

instead str.substring/indexOf works in chrome

Aren't these methods are part of standard String object?

Upvotes: 1

Views: 2708

Answers (3)

GrayedFox
GrayedFox

Reputation: 2563

This is a duplicate of this question.

I will repost the answer here for convenience.

Support for this, in Firefox and Chrome too, is now disabled by default. If you land here looking for an up to date answer, you can view the reason why, and the new method name (which is String.includes) here.

Try:

yourString.includes('searchString')

Upvotes: 1

Prasanjit Dey
Prasanjit Dey

Reputation: 6014

.contains() is not part of the standard string objects

Upvotes: 0

Ilya Skiba
Ilya Skiba

Reputation: 21

It is explained here. String.prototype.contains is not a part of EsmaScript 5.1, which is currently releases.

Upvotes: 1

Related Questions