Home > Web Front-end > JS Tutorial > body text

What are the differences between indexof and includes?

DDD
Release: 2023-11-24 13:20:24
Original
1658 people have browsed it

The differences are: 1. Different return value types; 2. Different accepted parameters; 3. Different ways of treating NaN; 4. Different ways of treating upper and lower case; 5. Different data types that can be used.

What are the differences between indexof and includes?

indexof and includes are methods in JavaScript used to determine whether a string contains a specified string. The differences between them are mainly reflected in the following aspects:

Return value type:

indexOf method returns the index position of the first character of the matching string, If no matching string is found, -1 is returned; the

includes method returns a Boolean value indicating whether a matching string is found.

Parameters:

The indexOf method accepts two parameters. The first is the string to be found, and the second is an optional parameter indicating the position to start searching. , the default is 0; the

includes method only accepts one parameter, which is the string to be found.

How to treat NaN:

The indexOf method cannot accurately determine whether NaN is in the string, it will treat NaN as equal to other NaN, so the return value may Not as expected; the

includes method can accurately determine whether NaN is in the string, and the return value is as expected.

How to treat upper and lower case:

The indexOf method is case-sensitive when matching, that is, uppercase letters and lowercase letters are treated as different characters;

# The ##includes method is not case-sensitive, that is, uppercase letters and lowercase letters are treated as the same characters.

Data types that can be used:

indexOf method can be used for strings, arrays and array-like objects;

includes method can be used for characters Strings and arrays.

When using indexof and includes, you need to choose the appropriate method according to the specific situation. If you only need to determine whether the string contains the specified string and do not care about the specific location, you can use the includes method. If you need to get the position of the matching string, you need to use the indexOf method. In addition, pay attention to the handling of NaN and case distinction when using the indexOf method.

The above is the detailed content of What are the differences between indexof and includes?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template