Simply talking about the IndexOf method in JS array _javascript skills

Source: Internet
Author: User

Objective

Believe that when it comes to indexOf everyone is not unfamiliar, to determine whether the string contains substrings are particularly common, is not skilled students of the sharp weapon. This article on the recent encounter of a problem, with an example to talk about the IndexOf method. This article is the accumulation of small knowledge, not as a topic of in-depth discussion, so there is no explanation for the second parameter of indexOf () , I believe we all know the role of the second parameter.

Use of String types

Brush up on the familiar string usage, and give a

Let str = ' orange ';

Str.indexof (' O '); 0
Str.indexof (' n ');//3
str.indexof (' C ');//-1

Here 0 and 3 are where O and n appear in the string. The starting subscript is 0. And-1 represents the mismatch.

Someone once asked me why-1 is not null or undefined . You ask the person who made the rules! A face helpless.

You see there's no bright spot here, don't worry. Another example

Let Numstr = ' 2016 ';

Numstr.indexof (' 2 '); 0
Numstr.indexof (2);//0

The point here is that IndexOf would do a simple type conversion, convert the number to a string ' 2 ' and then execute it.

Use of the number type

You may wonder number if the type has indexOf method because it will do the implicit conversion! Clearly tell everybody no, on the example

Let num = 2016;

Num.indexof (2); Uncaught TypeError:num.indexOf is not a function

Do you want to number use the IndexOf method on the type? Then it's converted into a string, and then the example goes on to write

The two forced youth's writing
num = ' 2016 ';
Num.indexof (2); 0

//Ordinary youth Writing
num.tostring (). IndexOf (2);//0

//Literary Youth (
' + num '). INDEXOF (2);//0

The first is simple and straightforward, and it is not not a good way to know a shorter number. But num what happens when variables are changed for different data? ❌

The second type is most commonly used, but a little longer than the third. Haha, in fact all can, code neat people like the third kind of ✅

Use of Array types

Everybody mentions the spirit, Big Boss comes.

The array method is familiar, but ignores the IndexOf method (I personally feel).

Do not practise nonsense, what problems, pay attention to ⚠️ Point and where?

Let arr = [' orange ', ' 2016 ', ' 2016 '];

Arr.indexof (' orange '); 0
arr.indexof (' o ');//-1

arr.indexof (' 2016 ');//1 arr.indexof
(2016);//-1

The example is not as thin as this one, and four use cases suffice to illustrate the problem.

arr.indexOf(‘orange') output 0 because ' orange ' is the No. 0 element of the array, matches to and returns the subscript.

arr.indexOf(‘o')output-1 because this method does not perform a indexOf match again on a per-element basis.

arr.indexOf(‘2016')Output 1 Because this method matches from scratch until it matches to the following table that returns the first array element, instead of returning all the matching subscripts.

arr.indexOf(2016) output-1 Note: No implicit type conversions are done here.

Now that the pit has been discovered, we might as well find out. Go to MDN official website to see exactly. Friends interested in this topic can jump directly to Array.prototype.indexOf ()

Just want to know the friends below to the official Description.

IndexOf () compares searchelement to elements of the Array using strict equality (the same method used by the = = or triple -equals operator).

At a glance, here is strictly equal to (= = =). Pay more attention when you make similar judgments. Don't mistake a number for a string, and the same string does not convert to a number.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.