0

I saw the use of an .equals() method in a Stackoverflow posting:

Any way to make jQuery.inArray() case insensitive?

var matchString = "MATCHME";
var rslt = null;
$.each(['foo', 'bar', 'matchme'], function(index, value) { 
  if (rslt == null && value.toLowerCase().equals(matchString.toLowerCase())) {
    rslt = index;
    return false;
  }
});

Yet I cannot find any documentation on this method. Any references would help.

Community
  • 1
  • 1
Robert
  • 10,126
  • 19
  • 78
  • 130
  • 7
    it's not a standard JS function – Alnitak Apr 20 '16 at 03:10
  • it is java if i am not mistaken [link](http://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java) – guradio Apr 20 '16 at 03:10
  • 1
    Could be this http://processingjs.org/reference/String_equals_/ – anthonygore Apr 20 '16 at 03:11
  • 1
    @anthonygore quite possibly - it could be from any library that adds that function to `String.prototype`. – Alnitak Apr 20 '16 at 03:12
  • 1
    The answer that proposed this code needs some serious clue-sticking – Alnitak Apr 20 '16 at 03:13
  • If you're using `str.equals()` in Javascript, there is no such standard method in Javascript so you must have some 3rd party library that has created that method. One could only answer this question if we knew what library that was. – jfriend00 Apr 21 '16 at 07:03
  • @jfriend00 that code came from another answer (since corrected) that didn't declare what libraries it was using – Alnitak Apr 21 '16 at 15:25
  • @Alnitak - Well the question is fairly worthless without a source for the function. – jfriend00 Apr 21 '16 at 22:47
  • If you're using the Enzyme library to test a JavaScript React app, it could be from that. https://github.com/airbnb/enzyme/blob/master/docs/api/ShallowWrapper/equals.md – Jon Schneider Jan 25 '18 at 21:42

4 Answers4

4

The code that you've seen in that other answer is not standard JS code.

It must have been lifted from a page that includes other code that adds the .equals() method to the String class (e.g. the "Processing.js" library) with code such as:

String.prototype.equals = function(that) {
    return this === that;
}

I've corrected the original answer so that it no longer has that dependency.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
-3

.equals is a Java Function.

Use operators in javascript.

if( a ==  b){ alert("hello") }

Your code is essentially correct just change the operators;

if(rslt == null && value.toLowerCase() == matchString.toLowerCase()){
}
guradio
  • 15,524
  • 4
  • 36
  • 57
-4

Here,

== does datatype comparison and .equals() will always does content comparison. So its better do content comparison always.

you can understand better below, == is to check identity. i.e: str1==str2 checks both are the same object or not. if yes returns true else returns false.

.Equals() is to check value. i.e: str1.Equals(str2) checks whther both holding identical values

Prabakaran
  • 47
  • 8
-5

.equals() is used to compare Strings in java, whereas == is used for int,double and float.

Bobin
  • 1
  • 1