Using indexOf (JavaScript) when working with arrays and strings

JavaScript is a unique programming language and an integral component of the browser, its syntax and semantics so organically embody the best traditions of modern algorithm development that they are in many ways an example for other languages ​​that present similar capabilities in a more cumbersome version.

indexof javascript

JavaScript's indexOf method does not position itself as a search operation, but it can be used to determine where a character or substring is in a string. The same method applies to regular arrays.

General description of the method

The general style of JavaScript syntax determines that strings and arrays have an indexOf method that calculates where the desired substring (or just a character) begins in the string where this method was applied.

In the context of the general logic of the language, a number can be automatically converted to a string data type. The language provides the programmer with a “var” descriptor without the ability to specify which type of data should be described. The ability to manipulate data types is a great JavaScript quality.

When describing a variable, the programmer can indicate the initial value and, accordingly, set its type, but at the right time this type will change in accordance with the requirements of the design or expression where the variable will be used.

Method Application Example

Three variables are described: a character string - "str", a regular array - "arr" and an associative array - "ass". There is a div on the web page with id = 'scCurrInfo', in which the result will be placed. The JavaScript indexOf method provides for strings and regular arrays.

All examples of selecting (searching) substrings are given in equivalent use. The source data are presented in the same way.

Author's example is not a picture

Using an indexOf array in JavaScript is like a purely lowercase version: if this array were represented by a string, the effect would be similar. Although the row representation of the data is, in many cases, more efficient than using arrays, using the indexOf method for data of type array also makes sense.

The result of executing the above code shows the obvious correspondence between applying the method to regular arrays and the classic JavaScript variant: indexOf string.

Author's example is not a picture

Associative arrays are distinguished by their nature and relate to the special logic of object-oriented programming in this language. The indexOf method is not applicable to them.

Rules for applying the method

Regardless of what (the string, the array) the indexOf method is used, JavaScript allows you to specify the second parameter as a number: the position after which you should look for the occurrence of the desired substring (character).

The search always goes before the first occurrence, that is, the result of the method will be a number: position (array index), from which the desired substring (character) begins. The position and index of the array starts from zero.

If nothing is found in the string or array, the result will be a negative value - “-1”.

javascript indexof array

The syntax of the language allows any syntactically correct combination of methods and the results of their execution. Simply put, there is no need to introduce new variables. You can use the method in any combination with other methods. You can get any text element, an array of divs, or another set of data from the DOM tree, and use indexOf JavaScript to exactly match its purpose.

Effective Data Processing Options

The syntax and semantics of JavaScript, especially with regard to object-oriented programming, deserve respect, however this does not prevent the developer from formulating his algorithms according to his “own semantics”.

Any task carries a unique meaning that can be laid down in the data structures proposed by the language and the design of their processing. But it is much easier to build the objects of the problem being solved, and in their terms to formulate its solution.

This is not only a more effective result, but also a more understandable algorithm, fewer errors in the code and good modifiability of the result afterwards: there will be no need to recall what was done and why.

javascript indexof string

In programming, clear and readable code is very important, especially when it will be further developed after some time.

In this context, the programmer can present data in ordinary lines, place the lines in objects, and place the semantic load according to methods that will select the necessary elements through indexOf at the right time.

Source: https://habr.com/ru/post/K17498/


All Articles