Built-in Method
NIKHIL KALARIYA
Posted on November 14, 2024
Here’s a brief overview of some commonly used array methods in JavaScript, with examples:
- push() Adds one or more elements to the end of an array.
let arr = [1, 2, 3];
arr.push(4); // Adds 4 to the end
console.log(arr); // Output: [1, 2, 3, 4]
- pop() Removes the last element from an array and returns it.
let arr = [1, 2, 3];
let removed = arr.pop(); // Removes 3
console.log(arr); // Output: [1, 2]
console.log(removed); // Output: 3
- shift() Removes the first element from an array and returns it
let arr = [1, 2, 3];
let removed = arr.shift(); // Removes 1
console.log(arr); // Output: [2, 3]
console.log(removed); // Output: 1
- unshift() Adds one or more elements to the beginning of an array.
let arr = [1, 2, 3];
arr.unshift(0); // Adds 0 at the start
console.log(arr); // Output: [0, 1, 2, 3]
- concat() Merges two or more arrays and returns a new array.
let arr1 = [1, 2];
let arr2 = [3, 4];
let combined = arr1.concat(arr2);
console.log(combined); // Output: [1, 2, 3, 4]
- slice() Returns a shallow copy of a portion of an array into a new array, without modifying the original array.
let arr = [1, 2, 3, 4, 5];
let sliced = arr.slice(1, 3); // Extracts elements from index 1 to 2
console.log(sliced); // Output: [2, 3]
- splice() Adds or removes elements from an array and modifies the original array.
let arr = [1, 2, 3, 4];
arr.splice(1, 2, 'a', 'b'); // Removes 2 elements from index 1 and adds
console.log(arr); // Output: [1, 'a', 'b', 4]
- forEach() Executes a provided function once for each array element.
let arr = [1, 2, 3];
arr.forEach(num => console.log(num * 2)); // Output: 2, 4, 6
- map() Creates a new array by applying a function to each element in the array.
let arr = [1, 2, 3];
let doubled = arr.map(num => num * 2);
console.log(doubled); // Output: [2, 4, 6]
- filter() Creates a new array with elements that pass a condition.
let arr = [1, 2, 3, 4];
let even = arr.filter(num => num % 2 === 0);
console.log(even); // Output: [2, 4]
- reduce() Applies a function to accumulate the array values into a single result.
let arr = [1, 2, 3, 4];
let sum = arr.reduce((acc, num) => acc + num, 0);
console.log(sum); // Output: 10
- find() Returns the first element that satisfies a given condition.
let arr = [1, 2, 3, 4];
let found = arr.find(num => num > 2);
console.log(found); // Output: 3
- findIndex() Returns the index of the first element that satisfies a given condition.
let arr = [1, 2, 3, 4];
let index = arr.findIndex(num => num > 2);
console.log(index); // Output: 2
- includes() Checks if an array includes a certain element.
let arr = [1, 2, 3];
console.log(arr.includes(2)); // Output: true
- sort() Sorts the elements of an array in place and returns the sorted array.
let arr = [3, 1, 4, 2];
arr.sort();
console.log(arr); // Output: [1, 2, 3, 4]
Here’s a brief overview of some commonly used string methods in JavaScript, along with examples:
- charAt() Returns the character at a specified index in a string.
let str = "Hello";
console.log(str.charAt(1)); // Output: "e"
- concat() Combines two or more strings and returns a new string.
let str1 = "Hello";
let str2 = "World";
console.log(str1.concat(" ", str2)); // Output: "Hello World"
- includes() Checks if a string contains a specified substring.
let str = "Hello World";
console.log(str.includes("World")); // Output: true
- indexOf() Returns the index of the first occurrence of a specified value. Returns -1 if not found.
let str = "Hello World";
console.log(str.indexOf("World")); // Output: 6
- lastIndexOf() Returns the index of the last occurrence of a specified value.
let str = "Hello World World";
console.log(str.lastIndexOf("World")); // Output: 12
- slice() Extracts a section of a string and returns it as a new string.
let str = "Hello World";
console.log(str.slice(0, 5)); // Output: "Hello"
- substring() Returns a substring between two specified indices.
let str = "Hello World";
console.log(str.substring(0, 5)); // Output: "Hello"
- substr() Returns a substring, starting at a specified position and with a specified length.
let str = "Hello World";
console.log(str.substr(6, 5)); // Output: "World"
- toUpperCase() Converts a string to uppercase letters.
let str = "Hello World";
console.log(str.toUpperCase()); // Output: "HELLO WORLD"
- toLowerCase() Converts a string to lowercase letters.
let str = "Hello World";
console.log(str.toLowerCase()); // Output: "hello world"
- trim() Removes whitespace from both ends of a string.
let str = " Hello World ";
console.log(str.trim()); // Output: "Hello World"
- replace() Replaces a specified value with another value in a string (replaces only the first occurrence).
let str = "Hello World";
console.log(str.replace("World", "Everyone")); // Output: "Hello Everyone"
- replaceAll() Replaces all occurrences of a specified value with another value in a string.
let str = "Hello World World";
console.log(str.replaceAll("World", "Everyone")); // Output: "Hello Everyone Everyone"
- split() Splits a string into an array of substrings, based on a specified separator.
let str = "Hello World";
console.log(str.split(" ")); // Output: ["Hello", "World"]
- repeat() Returns a new string that is a specified number of copies of the original string.
let str = "Hello";
console.log(str.repeat(3)); // Output: "HelloHelloHello"
- startsWith() Checks if a string begins with specified characters.
let str = "Hello World";
console.log(str.startsWith("Hello")); // Output: true
- endsWith() Checks if a string ends with specified characters.
let str = "Hello World";
console.log(str.endsWith("World")); // Output: true
- charCodeAt() Returns the Unicode of the character at a specified index.
let str = "Hello";
console.log(str.charCodeAt(0)); // Output: 72 (Unicode for "H")
- padStart() Pads the beginning of a string with another string until it reaches a specified length.
let str = "5";
console.log(str.padStart(3, "0")); // Output: "005"
- padEnd() Pads the end of a string with another string until it reaches a specified length.
let str = "5";
console.log(str.padEnd(3, "0")); // Output: "500"
💖 💪 🙅 🚩
NIKHIL KALARIYA
Posted on November 14, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.