Built-in Method

nikhil_kalariya_277019f8e

NIKHIL KALARIYA

Posted on November 14, 2024

Built-in Method

Here’s a brief overview of some commonly used array methods in JavaScript, with examples:

  1. 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]
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  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]
Enter fullscreen mode Exit fullscreen mode
  1. 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]
Enter fullscreen mode Exit fullscreen mode
  1. 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]

Enter fullscreen mode Exit fullscreen mode
  1. 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]
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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]
Enter fullscreen mode Exit fullscreen mode
  1. 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]
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. includes() Checks if an array includes a certain element.
let arr = [1, 2, 3];
console.log(arr.includes(2)); // Output: true
Enter fullscreen mode Exit fullscreen mode
  1. 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]
Enter fullscreen mode Exit fullscreen mode

Here’s a brief overview of some commonly used string methods in JavaScript, along with examples:

  1. charAt() Returns the character at a specified index in a string.
let str = "Hello";
console.log(str.charAt(1)); // Output: "e"
Enter fullscreen mode Exit fullscreen mode
  1. 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"
Enter fullscreen mode Exit fullscreen mode
  1. includes() Checks if a string contains a specified substring.
let str = "Hello World";
console.log(str.includes("World")); // Output: true
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. lastIndexOf() Returns the index of the last occurrence of a specified value.
let str = "Hello World World";
console.log(str.lastIndexOf("World")); // Output: 12
Enter fullscreen mode Exit fullscreen mode
  1. 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"
Enter fullscreen mode Exit fullscreen mode
  1. substring() Returns a substring between two specified indices.
let str = "Hello World";
console.log(str.substring(0, 5)); // Output: "Hello"
Enter fullscreen mode Exit fullscreen mode
  1. 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"
Enter fullscreen mode Exit fullscreen mode
  1. toUpperCase() Converts a string to uppercase letters.
let str = "Hello World";
console.log(str.toUpperCase()); // Output: "HELLO WORLD"
Enter fullscreen mode Exit fullscreen mode
  1. toLowerCase() Converts a string to lowercase letters.
let str = "Hello World";
console.log(str.toLowerCase()); // Output: "hello world"
Enter fullscreen mode Exit fullscreen mode
  1. trim() Removes whitespace from both ends of a string.
let str = "   Hello World   ";
console.log(str.trim()); // Output: "Hello World"
Enter fullscreen mode Exit fullscreen mode
  1. 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"
Enter fullscreen mode Exit fullscreen mode
  1. 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"
Enter fullscreen mode Exit fullscreen mode
  1. 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"]
Enter fullscreen mode Exit fullscreen mode
  1. 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"
Enter fullscreen mode Exit fullscreen mode
  1. startsWith() Checks if a string begins with specified characters.
let str = "Hello World";
console.log(str.startsWith("Hello")); // Output: true
Enter fullscreen mode Exit fullscreen mode
  1. endsWith() Checks if a string ends with specified characters.
let str = "Hello World";
console.log(str.endsWith("World")); // Output: true
Enter fullscreen mode Exit fullscreen mode
  1. charCodeAt() Returns the Unicode of the character at a specified index.
let str = "Hello";
console.log(str.charCodeAt(0)); // Output: 72 (Unicode for "H")
Enter fullscreen mode Exit fullscreen mode
  1. 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"
Enter fullscreen mode Exit fullscreen mode
  1. 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"
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
nikhil_kalariya_277019f8e
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.

Related