Code has been added to clipboard!


Reading time 2 min
Published Sep 8, 2017
Updated Oct 10, 2019

JavaScript test(): Main Tips

  • This method is used to test for a match inside a string variable.
  • The JavaScript test() method will return true if a match is found, just like a regular conditional statement. In other case, it will return false.

test() Function Explained

JavaScript test() is considered to be the most optimal method to find matches in a string. By mastering this JavaScript tester, you will able to retrieve information about various string patterns. Specifically, you should apply the test JavaScript method when you want to check strings for specific patterns within them.

Once the string is thoroughly searched, the JavaScript test() function will return a boolean value. If the string contains the characters you typed in, the method will return true. When the string does not contain any matches, false is

The following code snippet illustrates the way the .test JavaScript method searches a string. In this example, we are looking for the letter t in the specified string:

var sampleString = "Testing testing";
var samplePattern = new RegExp("t");
var sampleResult = samplePattern.test(sampleString);

Write The Method Correctly

The syntax of JavaScript .test function will not give you a headache. If you have at least a tiny bit of experience in programming, you will quickly grasp the way the function needs to be included in your code:


To use this JavaScript tester successfully, you need to include two values. To put it simply, you need to specify what to look for and where.

First, regExpObject defines the pattern you will be looking for. Then, there is a required parameter called string which indicates the string to be searched for specified characters or words.

What Method Returns

As we have mentioned before, the JavaScript .test function will deliver a boolean value. As you remember from learning JavaScript data types, those can either be true or false.

In our case, true indicates that matches of the pattern you specified were successfully found. However, if the outcome is false, that means no matches were detected in a string.

Web Development Course:

Web Development Course

Certificate Included
900h of content
For beginners