contains method is used in JavaScript to check whether a string contains another string and returns a Boolean value (true or false). The syntax is: string.contains(substring), where string is the target string and substring is the substring to be found.
Using the contains method in JavaScript
What is the contains method?
contains method is used to check whether a string contains another string and returns a Boolean value: true or false.
Syntax
<code class="js">string.contains(substring)</code>
Where:
string
is the target string to be checked. substring
is the substring to be found. Usage
The use of the contains method is very simple:
contains
method, passing the substring as a parameter. Example:
<code class="js">// 检查字符串是否包含另一个字符串 const message = "Hello, world!"; const result = message.contains("world"); console.log(result); // 输出:true</code>
Notes
null
or undefined
, the contains method will return false
. Alternative methods
Besides the contains method, there are other ways to check if a string contains another string:
indexOf
: If the substring exists, return the index of the first occurrence of the substring in the target string, otherwise return -1. includes
: Similar to contains, but it accepts an optional starting index parameter. The above is the detailed content of How to use contains in js. For more information, please follow other related articles on the PHP Chinese website!