JavaScript length examples

The length of a string, number, or other element is significant. The peculiarity of the JavaScript length method and the meaning of the concept of length is the basic functionality with the possibility of multifaceted application.

The developer can put his own idea of ​​the length of the value of a variable, array, or object into the length method.

Method Syntax and Application Rules

Basically, the length is a string and an array. In the first case, the number of characters is considered, in the second case, the number of elements. A number can also have a length, but in the context of its calculation, the number is converted to a symbolic representation.

An object usually has its own method for calculating the length, because its structure can consist of various elements, a simple summation of the lengths of which will show little: here JavaScript length & string length is a very relative concept. Although objects have a very careful attitude to the string: every object must fit into the string and get out of it - the traditional requirement of serialization / deserialization.

Different examples of length calculation

In this example, the first two expressions (1 and 2) compute the length of the string 'test' represented by a sequence of characters and an array of characters. Expression (3.1) shows the real string length with the usual JavaScript length, and expression (3.2) first breaks the string into an array by a space character, then calculates the length of each element and adds the number of spaces between the elements to the sum.

Custom solutions and a new sense of length

Typically, data is represented by semantic groups, so just working with strings almost never makes sense. It is more convenient to represent the processed string, at least as an array. It is better when it immediately appears as an object with a specific meaning.

By representing the string as an array, you can assign an anonymous function to calculate the length of an element. Naturally, there is no guarantee that the developer will understand by length its usual meaning.

In a particular application, the task can be set to consider the length of the number of characters without spaces or the number of words in a sentence. An anonymous function easily copes with the specific requirements of any task.

length javascript string length

So the meaning of the concept of "length" is determined by the actual application. In addition to such simple cases, the task often arises - to calculate the length according to dynamically changing rules. For example, adaptive layout changes the size of the browser window and CSS rules reduce the size of the displayed character - there is no direct sense in counting the length in characters, but the length of the line is important when displayed in pixels.

A realistic approach to length

In information processing, the length in units of which this information is built is important:

  • characters (only in this case, you can use JavaScript length without correction);
  • the words;
  • phrases;
  • offers, etc.

At the same time, when displaying information, the size of the element in which it is placed, the size of the symbol, and the length at which the desired line must be cut off for its correct display is important.

In each case, JavaScript length allows the developer to have basic obvious functionality and the ability to adapt it to the desired solution.


All Articles