Skip to content

Commit e7c0142

Browse files
authored
Topic 4: Add Splice vs Slice vs Split
1 parent 4f98ab3 commit e7c0142

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ One place JavaScript!
77
- [Callback/ Higher-order Function](#callback)
88
- [Promises](#promises)
99
- [Closures](#closures)
10+
- [Splice vs Slice vs Split](#SDiff)
1011

1112
<a name=“callback”/>
1213

@@ -61,3 +62,57 @@ function outerFunction () {
6162

6263
outerFunction()() // I see the outer variable!
6364
```
65+
66+
<a name=“closures”/>
67+
68+
#### Splice vs Slice vs Split
69+
70+
## Splice:
71+
The splice() method adds or removes items to or from an array, and returns the removed item(s).
72+
73+
NOTE: The original array will be changed by this Method.
74+
```javascript
75+
Syntax: array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
76+
77+
let car = ["Audi", "BMW", "Bajaj", "Aston Martin"];
78+
car.splice(1, 0, "Chevrolet", "Chrysler");
79+
80+
car;
81+
(6) ["Audi", "Chevrolet", "Chrysler", "BMW", "Bajaj", "Aston Martin"] // Added Chevrolet to car array at index of 1 and followed by Chrysler. Since Second argument is 0, none of the item is removed from an array.
82+
let result = car.splice(1, 1); ["Chevrolet"] // Returns the removed item
83+
car
84+
(5) ["Audi", "Chrysler", "BMW", "Bajaj", "Aston Martin"]
85+
```
86+
87+
## Slice:
88+
The slice() method returns the selected elements in an array, as a new array object.
89+
It selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.
90+
91+
NOTE: The original array will not be changed. It can be used with String as well.
92+
```javascript
93+
Syntax: arr.slice([begin[, end]])
94+
95+
let fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
96+
let citrus = fruits.slice(1, 3);
97+
98+
fruits
99+
(5) ["Banana", "Orange", "Lemon", "Apple", "Mango"]
100+
citrus
101+
(2) ["Orange", "Lemon"] // Doesn't included the end element- Apple in an result array.
102+
```
103+
104+
## Split:
105+
The split() method is used to split a string into an array of substrings, and returns the new array.
106+
107+
Note: The split() method does not change the original string.
108+
109+
USE: Reverse String, Substring
110+
```javascript
111+
Syntax: str.split([separator[, limit]])
112+
113+
let str = "How are you doing today?";
114+
let res = str.split(" ");
115+
116+
res
117+
(5) ["How", "are", "you", "doing", "today?"]
118+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy