JavaScript - at() method

revision:


Category: array

The at() method 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.

Syntax :

        array.at(index)    
    

Parameters:

index : zero-based index of the array element to be returned, converted to an integer. Negative index counts back from the end of the array — if "index < 0", "index + array.length" is accessed.

Examples:

            // Our array with items
            const cart = ["apple", "banana", "pear"];
            // A function which returns the last item of a given array
            function returnLast(arr) {
                return arr.at(-1);
            }
            // Get the last item of our array 'cart'
            const item1 = returnLast(cart);
            console.log(item1); // 'pear'
            // Add an item to our 'cart' array
            cart.push("orange");
            const item2 = returnLast(cart);
            console.log(item2); // 'orange'

            const sentence = 'The quick brown fox jumps over the lazy dog.';
            let index = 5;
            console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
            // Expected output: "Using an index of 5 the character returned is u"
            index = -4;
            console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
            // Expected output: "Using an index of -4 the character returned is d"

        

Category : string

The at() method returns an indexed character from a string. The at() method returns the same as [].

Syntax :

        string.at(index)    
    

Parameters:

index : optional. The index (position) of the character to be returned. Default is 0. -1 returns the last character.

Examples:

        const sentence = 'The quick brown fox jumps over the lazy dog.';
        let index = 5;
        console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
        // Expected output: "Using an index of 5 the character returned is u"
        index = -4;
        console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
        // Expected output: "Using an index of -4 the character returned is d"
    

Practical examples

example: using the at() method on arrays

code:
                    <div>
                        <p id="at-1"></p>
                        <p id="at-2"></p>
                        <p id="at-3"></p>
                    </div>
                    <script>
                        const array1 = [5, 12, 8, 130, 44];
                        let index = 2;
                        document.getElementById("at-1").innerHTML = "original array: " + array1;
                        console.log(`Using an index of ${index} the item returned is ${array1.at(index)}`);
                        // Expected output: "Using an index of 2 the item returned is 8"
                        document.getElementById("at-2").innerHTML = `Using an index of ${index} the item returned is ${array1.at(index)}`;
                        index = -2;
                        console.log(`Using an index of ${index} item returned is ${array1.at(index)}`);
                        // Expected output: "Using an index of -2 item returned is 130"
                        document.getElementById("at-3").innerHTML = `Using an index of ${index} the item returned is ${array1.at(index)}`;
                        index = -2;
                    </script>
                

example: using the at() method on arrays

code:
                    <div>
                        <p id="at-4"></p>
                        <p id="at-5"></p>
                        <p id="at-6"></p>
                    </div>
                    <script>
                        const basket = ["orange", " bleuberries", " peanuts"];
                        document.getElementById("at-4").innerHTML = "our basket: " + basket;
                        function returnLast(arr) {
                            return arr.at(-1);
                        }
                        const item1 = returnLast(basket);
                        console.log(item1); // 'pear'
                        document.getElementById("at-5").innerHTML = "last  item in basket: " + item1;
                        basket.push("orange");
                        const item2 = returnLast(basket);
                        console.log(item2); // 'orange'
                        document.getElementById("at-6").innerHTML = "item added in basket: " + item2;
                    </script>
                

example: using the at() method on strings

code:
                    <div>
                        <p id="at-7"></p>
                        <p id="at-8"></p>
                        <p id="at-9"></p>
                        <p id="at-10"></p>
                        <p id="at-11"></p>
                        <p id="at-12"></p>
            
                    </div>
                    <script>
                        let text = "my Website";
                        document.getElementById("at-7").innerHTML = "string : " + text;
                        let character = text.at(0);
                        document.getElementById("at-8").innerHTML = "first character : " + character;
                        let character1 = text[0];
                        document.getElementById("at-9").innerHTML = "first character : " + character1;
                        let character2 = text.at();
                        document.getElementById("at-10").innerHTML = "character : " + character2;
                        let character3 = text.at(-1);
                        document.getElementById("at-11").innerHTML = "last character : " + character3;