|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Commonly used JavaScript String, Array, and Object methods" |
| 4 | +date: 2019-11-28 10:00:00 +0530 |
| 5 | +tags: [JavaScript, functions, methods] |
| 6 | +comments: true |
| 7 | +--- |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +In this post, we will look at some commonly used JavaScript String, Array, and Object methods and some insights on choosing the right method based on the task. |
| 12 | + |
| 13 | +Clicking on a method name opens a new tab with its MDN documentation. You can refer it for more information and usage examples. |
| 14 | + |
| 15 | +## String methods |
| 16 | +Following are some of the common methods from the [String][String] global object. |
| 17 | + |
| 18 | +- [indexOf()][String.prototype.indexOf()] - This method returns the index of the passed string within the string. Returns -1 if the value is not found. |
| 19 | +- [includes()][String.prototype.includes()] - This method returns _true_ or _false_ based on whether the passed string exists within the string. |
| 20 | + |
| 21 | +Use `indexOf()` if you need the position of the passed string in the string. Use `includes()` if you are only concerned whether the passed string exists, it makes intent of the code more clear. |
| 22 | + |
| 23 | +- [trim()][String.prototype.trim()] - This method removes whitespace from both ends of a string. |
| 24 | +- [split()][String.prototype.split()] - This method returns an array of strings by separating the string at each instance of a specified separator string. |
| 25 | +- [substring()][String.prototype.substring()] - This method returns the part of the string between the start and end indexes. |
| 26 | +- [slice()][String.prototype.slice()] - This method is quite similar to `substring()` with some [differences][StackOverflow - slice() vs substring()]. |
| 27 | + |
| 28 | +[String]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String |
| 29 | +{:target="_blank"} |
| 30 | +[String.prototype.indexOf()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf |
| 31 | +{:target="_blank"} |
| 32 | +[String.prototype.includes()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes |
| 33 | +{:target="_blank"} |
| 34 | +[String.prototype.trim()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim |
| 35 | +{:target="_blank"} |
| 36 | +[String.prototype.split()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split |
| 37 | +{:target="_blank"} |
| 38 | +[String.prototype.substring()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring |
| 39 | +{:target="_blank"} |
| 40 | +[String.prototype.slice()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice |
| 41 | +{:target="_blank"} |
| 42 | +[StackOverflow - slice() vs substring()]: https://stackoverflow.com/a/2243835/2924577 |
0 commit comments