Skip to content

Instantly share code, notes, and snippets.

@vqc1909a
Last active November 12, 2025 02:43
Show Gist options
  • Select an option

  • Save vqc1909a/5dae09a7aa70d1003d06d3276661a151 to your computer and use it in GitHub Desktop.

Select an option

Save vqc1909a/5dae09a7aa70d1003d06d3276661a151 to your computer and use it in GitHub Desktop.
METHODS_JAVASCRIPT

METHODS JAVASCRIPT

It's a list of the common methods javascript for a technical tests

String

Method Description Example
.substring returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied. const str = "Mozilla";
str.substring(1, 3); // Expected output: "oz"
str.substring(2); // Expected output: "zilla"
.search string method that searches for a match between a regular expression and a string. It returns the index of the first match, or -1 if the match is not found. const str = "Hello world!";
const index = str.search(/world/); // returns 6
.match string method that searches for matches between a string and a regular expression. It returns an array of matches, or null if the match is not found. const paragraph = "The quick brown fox jumps over the lazy dog. It barked.";
const regex = /[A-Z]/g;
const found = paragraph.match(regex); // return ["T", "I"]
.includes string method that performs a case-sensitive search to determine whether a given string may be found within this string, returning true or false as appropriate. const str = "Hello world!";
str.includes("world", index); // returns true

Array

Method Description Example
.includes array method that determines whether an array includes a certain value among its entries, returning true or false as appropriate. const pets = ["cat", "dog", "bat"];
pets.includes("Cat", index); // return false
.indexOf array method that returns the first index at which a given element can be found in the array, or -1 if it is not present. indexOf searches forward from the given index, const numbers = [2, 5, 9, 2];
numbers.indexOf(2, 1); // returns 3
.lastIndexOf array method that returns the last index at which a given element can be found in the array, or -1 if it is not present. The second argument sets the starting point for the backward search. const numbers = [2, 5, 9, 2];
numbers.lastIndexOf(2, 3); // returns 3
.shift (WARNING => MUTABLE) array method that removes the first element from an array and returns that removed element. const array = [1, 2, 3];
const firstElement = array.shift(); // Expected output: 1
.pop (WARNING => MUTABLE) The pop() method of Array instances removes the last element from an array and returns that element. This method changes the length of the array. const plants = ["broccoli", "cauliflower", "cabbage", "kale", "tomato"];
plants.pop(); //Expected output: "tomato"
.unshift (WARNING => MUTABLE) array method that adds the specified elements to the beginning of an array and returns the new length of the array. const array = [1, 2, 3];
array.unshift(4, 5) // Expected output: 5
.fill (WARNING => MUTABLE) array method that changes all elements within a range of indices in an array to a static value. If you don't put any value, it would take the undefined value. const createBase = (width, height) => {
return Array(height).fill().map(() => Array(width).fill(0));
}
.find array method that returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned. const array = [5, 12, 8, 130, 44];
const found = array.find((element) => element > 10);
.findIndex array method that returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned. const array = [5, 12, 8, 130, 44];
array.findIndex((element) => element > 13;) // Expected output: 3
.at array method that takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array. const array = [5, 12, 8, 130, 44];
array.at(-2); // Expected output: 130
.slice array method that returns a shallow copy of a portion of an array into a new array object selected from start index to end index (end not included). The original array will not be modified. const animals = ["ant", "bison", "camel", "duck", "elephant"];
animals.slice(2); // Array ["camel", "duck", "elephant"]
animals.slice(2, -1);
// Array ["camel", "duck"]
.splice (WARNING => MUTABLE) array method that changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. const months = ["Jan", "March", "April", "June"];
months.splice(1, 0, "Feb"); console.log(months); // Array ["Jan", "Feb", "March", "April", "June"]
months.splice(4, 1, "May", "September"); // console.log(months); // Array ["Jan", "Feb", "March", "April", "May", "September"]
.sort (WARNING => MUTABLE) For each pair of elements a and b, the function returns: A negative value if a should come before b, Zero if their order doesn't matter, A positive value if a should come after b. const numbers = [3, 1, 4, 2];
numbers.sort((a, b) => a - b); // [1, 2, 3, 4]
numbers.sort((a, b) => Math.random() - 0.5); //Sort random numbers of array
.toSorted // const months = ["Mar", "Jan", "Feb", "Dec"];
const sortedMonths = months.toSorted();
sortedMonths; // ['Dec', 'Feb', 'Jan', 'Mar']
months; // ['Mar', 'Jan', 'Feb', 'Dec']
.toReversed Is a method of Array instances is the copying counterpart of the reverse() method. It returns a new array with the elements in reversed order. const reversedItems = items.toReversed();
reversedItems; // [3, 2, 1]
items; // [1, 2, 3]

Number

Method Description Example

Objects

Method Description Example
.freeze A frozen object can no longer be changed: new properties cannot be added, existing properties cannot be removed and the object's prototype cannot be re-assigned. freeze() returns the same object that was passed in. const obj = { prop: 42 };
Object.freeze(obj);
obj.prop = 33;
console.log(obj.prop); // Expected output: 42
.hasOwnProperty object method that returns a boolean indicating whether this object has the specified property as its own property (as opposed to inheriting it). const obj = { name: "Alice" };
obj.hasOwnProperty("name"); // returns true
obj.hasOwnProperty("toString"); // returns false (inherited from prototype)
.hasOwn static method that returns true if the specified object has the indicated property as its own property. If the property is inherited, or does not exist, the method returns false. const object = { prop: "exists" };
Object.hasOwn(object, "prop"); // true
.entries static method that returns an array of a given object's own enumerable string-keyed property key-value pairs. const obj = { foo: "bar", baz: 42 };
Object.entries(obj); // [ ['foo', 'bar'], ['baz', 42] ]
.fromEntries static method that transforms a list of key-value pairs into an object. const entries = new Map([["foo", "bar"], ["baz", 42]]);
const obj = Object.fromEntries(entries); // Object { foo: "bar", baz: 42 }
new Map() The Map object holds key-value pairs and remembers the original insertion order of the keys. const obj = { foo: "bar", baz: 42 };
const map = new Map(Object.entries(obj)); // Map(2) {"foo" => "bar", "baz" => 42}
new Set() Set objects are collections of values. const numbers3 = [2, 6, 4, 8, 8, 6, 4];
[...new Set(numbers3)]; Array.from(new Set(numbers3)) //[ 2, 6, 4, 8 ]
new Intl() Instead to use the join() method const formatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction',});
formatter.format(vehicles); //'Motorcycle, Bus, and Car'
.groupBy static method groups the elements of a given iterable according to the string values returned by a provided callback function. const inventory = [{ name: "asparagus", type: "vegetables", quantity: 5 }, { name: "bananas", type: "fruit", quantity: 0 }, { name: "goat", type: "meat", quantity: 23 }, { name: "cherries", type: "fruit", quantity: 5 }, { name: "fish", type: "meat", quantity: 22 }];
Object.groupBy(inventory, ({type}) => type);
Object.groupBy(inventory, ({quantity}) => {if(quantity < 5) return "no-stock"; if(quantity < 10) return "stock"; return "over-stock";})
Map.groupBy() static method groups the elements of a given iterable using the values returned by a provided callback function. const inventory = [{ name: "asparagus", type: "vegetables", quantity: 9 }, { name: "bananas", type: "fruit", quantity: 5 }, { name: "goat", type: "meat", quantity: 23 }, { name: "cherries", type: "fruit", quantity: 12 }, { name: "fish", type: "meat", quantity: 22 }];
const restock = { restock: true };
const sufficient = { restock: false };
const result = Map.groupBy(inventory, ({quantity }) => quantity < 6 ? restock : sufficient);
result.get(restock)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment