JavaScript Array and String common methods.

JavaScript is an superior language with loads of built-in objects (Array, Date, Math, and String) and their build-in strategies (break up, kind, parseInt) which make a developer’s life simpler.

Array -> Index-Worth pair
String -> Can also work as Index-Worth(character) pair

Let’s examine in particulars –

const jsString = 'abc'
Enter fullscreen mode

Exit fullscreen mode

will be thought of equal (for one dimension array solely) to –

const jsArray = ['a', 'b', 'c']
Enter fullscreen mode

Exit fullscreen mode

Instance:

jsString.indexOf('a') // 0
jsArray.indexOf('a')  // 0 or,

jsString[0] // a
jsArray[0]  // a

jsString == jsArray // false - as a result of array go reference
'a,b,c' == ['a', 'b', 'c'] // true (unfastened examine)
Enter fullscreen mode

Exit fullscreen mode

Widespread Strategies:
There are some helpful strategies these work on arrays in addition to in strings in javascript. So, for those who study them, can simply apply for each. These are –

.slice()
.indexOf()
.lastIndexOf()
.concat()
.consists of()
.size
.toString()
.valueOf()
[…strOrArray]

Like –

Bear in mind: A String all the time carries its worth whereas an array all the time carries reference. That is why there are some strategies(not all) of the array that adjustments the precise worth of the variable. However for string, there aren’t any strategies that may change its precise worth.


Array Strategies:
Right here listed some strategies for array that modifies the unique array worth straight. These strategies by no means will be utilized for string –

.splice()
.push()
.pop()
.shift()
.unshift()
.reverse()
.fill()

These array strategies do not modify the array however used just for array –

.map()
.filter
.scale back()


String Strategies:
Just for string-

.substr() // just like splice
.substring() // just like slice
.charAt()
.match()
.trim()


If you wish to use string strategies for an array, you’ll be able to convert that array to a string and vice versa you need to use array strategies for string by changing that string to an array. You may simply convert one another –

Split and Join

THANKS!

Add a Comment

Your email address will not be published. Required fields are marked *