Extract Matches VS Test

Tell us what’s happening:
I’m curious why when you use test() method, you use the the method as regex.test(str)

let regex = /Code/; 
let result = regex.test(str);

When you use match() method, you use the method as str.match(regex)

let regex= /Code/;
let result = str.match(regex);

The only difference between the 2 methods is test() returns a boolean and match() returns a string.

I’m asking what is actually happening under the hood and if there was a higher level reasoning for this i.e. returning a string requires that match() takes regex as argument. Why?

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/extract-matches

It is how they were implemented…
test() is a regex method (RexExp.prototype.test()), match is a string method (String.prototype.match())

There is not a particular reason, with object oriented programming a method can be of one thing or of the other and the result is the same.
It should be asked to who implement the methods in the language :sweat_smile:

1 Like

That’s good info though. I didn’t realize that methods were categorized like that. Narrows down my Googles. Thanks.

Also, do you know how many types of methods there are? I’m sure I can find the information. I was just curious if this is common knowledge.

Would you please expand on what you mean by “type”?

Well you mentioned that test() is a regex method and match() is a string method. This to me sounds like methods are classified into categories or types or whatever. I assume there are probably array methods and number methods.

So I was just curious how many types of methods there are. Are there less than a dozen? hundreds? thousands? Is there documentation where I can find this? Searching “Js methods” yields either a generic definition or some general description of a few different methods, but nothing that is encompassing.

Methods are for the built-in objects, so Object, Array, String, RegExp etc

You can check each of them here:

If you open one you will be able to check if they have built-in methods or not

1 Like

Just what I was looking for. Thank you sit.