Code has been added to clipboard!

test

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:

Example
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:

regExpObject.test(string)

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.

What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen