Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the indexOf method in JavaScript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use the indexOf method in JavaScript". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use the indexOf method in JavaScript" can help you solve the problem.

Use of the String type

For instance

Let str = 'orange'

Str.indexOf ('o'); / / 0

Str.indexOf ('n'); / / 3

Str.indexOf ('c'); / /-1

Where 0 and 3 are respectively where o and n appear in the string. The starting subscript is 0. And-1 means there is no match.

Someone once asked me why-1 is not null or undefined. Ask the person who makes the rules! With a helpless look on his face.

When you see it, you don't feel any bright spot here. Don't worry, let's go on with another example.

Let numStr = '2016'

NumStr.indexOf ('2'); / / 0

NumStr.indexOf (2); / / 0

One small point here is that indexOf does a simple type conversion, converting the number to the string'2' and then executing it.

Use of the Number type

You may wonder if there is an indexOf method for number types because they can do implicit conversions. Let me tell you clearly that no, the above example

Let num = 2016; num.indexOf (2); / / Uncaught TypeError: num.indexOf is not a function

Do you have to use the indexOf method on the number type? Then convert it to a string, and then write it in the above example.

/ / num = '2016 youth; num.indexOf (2); / / 0 / / ordinary youth num.toString (). IndexOf (2); / / 0 / / Literary youth (''+ num) .indexOf (2); / / 0

* the writing method is simple and straightforward, and it is not impossible for known shorter numbers. But what if the num variable changes for different data?

The second method is the most commonly used, but it is a little longer than the third one. Haha, actually, it's all right. Code cleanliness addicts like the third kind.

Use of the Array type

Everybody cheer up, here comes the big boss.

The array method is all too familiar to us, but it ignores the fact that the array has indexOf (my personal feeling).

If you don't practice nonsense, what problems do you encounter and where is the point of attention?

Let arr = ['orange',' 2016 years, '2016']; arr.indexOf ('orange'); / / 0 arr.indexOf (' o'); / /-1 arr.indexOf ('2016'); / / 1 arr.indexOf (2016); / /-1

The examples are not broken down in that detail here, and four use cases are enough to illustrate the problem.

Arr.indexOf ('orange') outputs 0 because' orange' is the 0th element of the array, matches to and returns the subscript.

Arr.indexOf ('o') outputs-1 because this method does not perform indexOf matching again on a per-element basis.

Arr.indexOf ('2016') outputs 1 because this method returns the following table of * * array elements from scratch until the match arrives, rather than returning all matching subscripts.

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

Now that the pit has been found, we might as well get to the bottom of it. Go to the MDN official website to see what's going on. Friends who are interested in this topic can jump directly to Array.prototype.indexOf ()

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

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

This is the end of the introduction to "how to use the indexOf method in JavaScript". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report