JavaScript

How to Check if String Contains Question Mark in JavaScript

At the last of any sentence or phrase, a question mark (?) is a punctuation mark utilized for representing a direct question. Sometimes, you need to verify whether there is any question mark in a text document or a paragraph. For instance, to validate the questions present in the text. JavaScript gives some built-in methods that help to do this task efficiently.

This tutorial will demonstrate the procedure for verifying the question mark in a string.

How to Check if String Contains Question Mark in JavaScript?

To check if a given string has a question mark, use the following methods.

Let’s use both of them one by one!

Method 1: Check if String Contains Question Mark Using includes() Method

To verify whether a question mark is present in a string, use the “includes()” method. It accepts a question mark as an argument and returns “true” if the question mark exists in the string, else it outputs “false”.

Syntax

Follow the below-mentioned method to use the includes() method:

string.includes(character);

Here, “character” can be a question mark (?) which will be checked in the specified string.

Example

In this example, we will create a string stored in a variable named “string”:

var string = "How to code a JavaScript program?";

Then, invoke the includes() method by passing a question mark as an argument and store the returned result in the variable “ans”:

var ans = string.includes('?');

Finally, print the resultant value using the “console.log()” method:

console.log(ans);

The output displayed “true” which indicates that the string contains a question mark(?):

Let’s head toward the second method!

Method 2: Check if String Contains Question Mark Using match() Method

Another method in JavaScript called the “match()” method checks whether a string contains a question mark or not. The match() method compares a string to a regular expression or a regex pattern. If a match occurs, an array of matches is returned; else the null is returned. The ternary operator or conditional statement can also be used with the match() method.

Syntax

Follow the given syntax for verifying the string contains a question mark using the match() method:

string.match(regexPattern);

Here, the “regexPattern” is the regular expression that will search for the question mark in the string.

Example

We will now use the same string created in the above example and use the ternary operator with the match() method by passing the regular expression to search for the question mark:

var ans = string.match(/\?/g) ? "true" : "false";

Print the result on the console:

console.log(ans);

As you can see, the output gives “true” which means the question mark (?) exist in the string:

We have gathered the simplest JavaScript methods for determining whether the string contains a question mark.

Conclusion

To verify whether the string contains a question mark, you can use JavaScript predefined methods, such as the includes() method or match() method. The includes() method searches for the question mark as a substring, whereas the match() method compares the string based on the given pattern. This tutorial demonstrated the procedure for verifying the question mark in a string with a detailed explanation.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.