🔧 Programmierung 🕛 vor 1 Jahr 5 Min Lesezeit
0

Built-in Method

↗ Quelle (dev.to)
🗣️ Stimme:

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.




CODE
let arr = [1, 2, 3];
arr.push(4); // Adds 4 to the end
console.log(arr); // Output: [1, 2, 3, 4]







  1. pop()
    Removes the last element from an array and returns it.




CODE
let arr = [1, 2, 3];
let removed = arr.pop(); // Removes 3
console.log(arr); // Output: [1, 2]
console.log(removed); // Output: 3







  1. shift()
    Removes the first element from an array and returns it




CODE
let arr = [1, 2, 3];
let removed = arr.shift(); // Removes 1
console.log(arr); // Output: [2, 3]
console.log(removed); // Output: 1







  1. unshift()
    Adds one or more elements to the beginning of an array.




CODE

let arr = [1, 2, 3];
arr.unshift(0); // Adds 0 at the start
console.log(arr); // Output: [0, 1, 2, 3]







  1. concat()
    Merges two or more arrays and returns a new array.




CODE
let arr1 = [1, 2];
let arr2 = [3, 4];
let combined = arr1.concat(arr2);
console.log(combined); // Output: [1, 2, 3, 4]







  1. slice()
    Returns a shallow copy of a portion of an array into a new array, without modifying the original array.




CODE
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]








  1. splice()
    Adds or removes elements from an array and modifies the original array.




CODE
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]







  1. forEach()
    Executes a provided function once for each array element.




CODE
let arr = [1, 2, 3];
arr.forEach(num => console.log(num * 2)); // Output: 2, 4, 6







  1. map()
    Creates a new array by applying a function to each element in the array.




CODE
let arr = [1, 2, 3];
let doubled = arr.map(num => num * 2);
console.log(doubled); // Output: [2, 4, 6]







  1. filter()
    Creates a new array with elements that pass a condition.




CODE
let arr = [1, 2, 3, 4];
let even = arr.filter(num => num % 2 === 0);
console.log(even); // Output: [2, 4]







  1. reduce()
    Applies a function to accumulate the array values into a single result.




CODE
let arr = [1, 2, 3, 4];
let sum = arr.reduce((acc, num) => acc + num, 0);
console.log(sum); // Output: 10







  1. find()
    Returns the first element that satisfies a given condition.




CODE
let arr = [1, 2, 3, 4];
let found = arr.find(num => num > 2);
console.log(found); // Output: 3







  1. findIndex()
    Returns the index of the first element that satisfies a given condition.




CODE
let arr = [1, 2, 3, 4];
let index = arr.findIndex(num => num > 2);
console.log(index); // Output: 2







  1. includes()
    Checks if an array includes a certain element.




CODE
let arr = [1, 2, 3];
console.log(arr.includes(2)); // Output: true







  1. sort()
    Sorts the elements of an array in place and returns the sorted array.




CODE
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:




  1. charAt()
    Returns the character at a specified index in a string.




CODE
let str = "Hello";
console.log(str.charAt(1)); // Output: "e"







  1. concat()
    Combines two or more strings and returns a new string.




CODE
let str1 = "Hello";
let str2 = "World";
console.log(str1.concat(" ", str2)); // Output: "Hello World"







  1. includes()
    Checks if a string contains a specified substring.




CODE
let str = "Hello World";
console.log(str.includes("World")); // Output: true







  1. indexOf()
    Returns the index of the first occurrence of a specified value. Returns -1 if not found.




CODE
let str = "Hello World";
console.log(str.indexOf("World")); // Output: 6







  1. lastIndexOf()
    Returns the index of the last occurrence of a specified value.




CODE
let str = "Hello World World";
console.log(str.lastIndexOf("World")); // Output: 12







  1. slice()
    Extracts a section of a string and returns it as a new string.




CODE
let str = "Hello World";
console.log(str.slice(0, 5)); // Output: "Hello"







  1. substring()
    Returns a substring between two specified indices.




CODE
let str = "Hello World";
console.log(str.substring(0, 5)); // Output: "Hello"







  1. substr()
    Returns a substring, starting at a specified position and with a specified length.




CODE
let str = "Hello World";
console.log(str.substr(6, 5)); // Output: "World"







  1. toUpperCase()
    Converts a string to uppercase letters.




CODE
let str = "Hello World";
console.log(str.toUpperCase()); // Output: "HELLO WORLD"







  1. toLowerCase()
    Converts a string to lowercase letters.




CODE
let str = "Hello World";
console.log(str.toLowerCase()); // Output: "hello world"







  1. trim()
    Removes whitespace from both ends of a string.




CODE
let str = "   Hello World   ";
console.log(str.trim()); // Output: "Hello World"







  1. replace()
    Replaces a specified value with another value in a string (replaces only the first occurrence).




CODE
let str = "Hello World";
console.log(str.replace("World", "Everyone")); // Output: "Hello Everyone"







  1. replaceAll()
    Replaces all occurrences of a specified value with another value in a string.




CODE
let str = "Hello World World";
console.log(str.replaceAll("World", "Everyone")); // Output: "Hello Everyone Everyone"







  1. split()
    Splits a string into an array of substrings, based on a specified separator.




CODE
let str = "Hello World";
console.log(str.split(" ")); // Output: ["Hello", "World"]







  1. repeat()
    Returns a new string that is a specified number of copies of the original string.




CODE
let str = "Hello";
console.log(str.repeat(3)); // Output: "HelloHelloHello"







  1. startsWith()
    Checks if a string begins with specified characters.




CODE
let str = "Hello World";
console.log(str.startsWith("Hello")); // Output: true







  1. endsWith()
    Checks if a string ends with specified characters.




CODE
let str = "Hello World";
console.log(str.endsWith("World")); // Output: true







  1. charCodeAt()
    Returns the Unicode of the character at a specified index.




CODE
let str = "Hello";
console.log(str.charCodeAt(0)); // Output: 72 (Unicode for "H")







  1. padStart()
    Pads the beginning of a string with another string until it reaches a specified length.




CODE
let str = "5";
console.log(str.padStart(3, "0")); // Output: "005"







  1. padEnd()
    Pads the end of a string with another string until it reaches a specified length.




CODE
let str = "5";
console.log(str.padEnd(3, "0")); // Output: "500"


Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
6 Quellen
CVE-2022-44255 | TOTOLINK LR350 9.3.5u.6369_B20220309 buffer overflow (EUVD-2022-47204)
2 Quellen
CVE-2026-68426 | Linux Kernel up to 6.18.41/7.1.5/7.2-rc3 xfrm validate_xmit_skb_list use after free (Nessus ID 346426)
1 Quelle
Windows 11 Probleme mit gültiger Domänenanmeldung nach September-Update [Workaround]
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Built-in Method

Thematisch verwandte Begriffe: Builtin, Method · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...