Skip to content

Instantly share code, notes, and snippets.

@dbsimeonov
Last active January 17, 2018 15:31
Show Gist options
  • Save dbsimeonov/fd39db63e43bd8b1346d489dec13e351 to your computer and use it in GitHub Desktop.
Save dbsimeonov/fd39db63e43bd8b1346d489dec13e351 to your computer and use it in GitHub Desktop.
Here I will share all of my notes for methods/functions/syntaxes/reference etc..

Splice vs Slice

Using .splice(); - When you use splice you are modify/editing the array - deleteCount is how many indexes you want to remove - itemN is if you want to replace the index with different content

*Syntax array.splice(start[, deleteCount[, item1[, item2[, ...]]]])

Using .slice(); - The main difference is that slice you don't modify the original array. Just cut it choosing the start & end index inside the brackets, returning you a copy of that new array. - In the brackets you can set only the starting point, and the end will be assigned auto as the end of the array

*Syntax arr.slice([begin[, end]])

ex.

var array=[1,2,3,4,5];
console.log(array.splice(2));
// shows [3, 4, 5], returned removed item(s) as a new array object.
 
console.log(array);
// shows [1, 2], original array altered.

//////////

var array=[1,2,3,4,5]
console.log(array.slice(2));
// shows [3, 4, 5], returned selected element(s).
 
console.log(array.slice(-2));
// shows [4, 5], returned selected element(s).
console.log(array);
// shows [1, 2, 3, 4, 5], original array remains intact.

Ref:

Substr vs Substring

Using .substr(); - Takes parametrs at (from, length) *Syntax str.substr(start[, length])

Using .substring(); - Takes parametrs (from, to) *Syntax str.substring(indexStart[, indexEnd])

ex.

const name = "Daniels"; 
console.log(name.substring(2, 5)); // Show in the console -> "nie"
console.log(name.substr(2,5)); // Show in the  console -> "niels"

Ref:

indexOf vs lastIndexOf

Using .indexOf();

  • The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.
  • fromIndex in the brackets is optional
  • It is case sensitive *Syntax str.indexOf(searchValue[, fromIndex])

ex.

 'Blue Whale'.indexOf('Blue');     // returns  0
 'Blue Whale'.indexOf('Blute');    // returns -1
 'Blue Whale'.indexOf('Whale', 0); // returns  5
 'Blue Whale'.indexOf('Whale', 5); // returns  5
 'Blue Whale'.indexOf('Whale', 7); // returns -1
 'Blue Whale'.indexOf('');         // returns  0
 'Blue Whale'.indexOf('', 9);      // returns  9
 'Blue Whale'.indexOf('', 10);     // returns 10
 'Blue Whale'.indexOf('', 11);     // returns 10

Using .lastIndexOf();

  • The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from fromIndex. Returns -1 if the value is not found.
  • Same thing as .indexOf(); but is searching backwards
  • fromIndex is optional
  • Case sensitive as well
    • Displays the last index that the string contains, check the example *Syntax str.lastIndexOf(searchValue[, fromIndex])

ex.

 'canal'.lastIndexOf('a');     // returns 3
 'canal'.lastIndexOf('a', 2);  // returns 1
 'canal'.lastIndexOf('a', 0);  // returns -1
 'canal'.lastIndexOf('x');     // returns -1
 'canal'.lastIndexOf('c', -5); // returns 0
 'canal'.lastIndexOf('c', 0);  // returns 0
 'canal'.lastIndexOf('');      // returns 5
 'canal'.lastIndexOf('', 2);   // returns 2

Ref:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment