Array -> Index-Value pair
String -> Also can work as Index-Value(character) pair
Let's see in details -
const jsString = 'abc'
can be considered equivalent (for one dimension array only) to -
const jsArray = ['a', 'b', 'c']
jsString.indexOf('a') // 0 jsArray.indexOf('a') // 0 or, jsString // a jsArray // a jsString == jsArray // false - because array pass reference 'a,b,c' == ['a', 'b', 'c'] // true (loose check)
Remember: A String always carries its value whereas an array always carries reference. That's why there are some methods(not all) of the array that changes the actual value of the variable. But for string, there are no methods that can change its actual value.
Here listed some methods for array that modifies the original array value directly. These methods never can be applied for string -
These array methods don't modify the array but used only for array -
Only for string-
.substr() // similar to splice
.substring() // similar to slice
If you want to use string methods for an array, you can convert that array to a string and vice versa you can use array methods for string by converting that string to an array. You can easily convert each other -
Top comments (2)