Access Array Elements from Any Side in JavaScript | JavaScript New Array Method Tutorial

Access Array Elements from Any Side in JavaScript | JavaScript New Array Method Tutorial

WebStylePress

55 лет назад

669 Просмотров

Access Array elements from left or right side, by using array length or without using array length. There is a new Array AT method in JavaScript that simplifies accessing Array elements from end of Array. Array is a variable that can hold more than one value. Simply it's a list of items. We can take a variable with few elements and look into JavaScript Arrays and JavaScript Array Methods. We will also learn JavaScript Array new method. You will learn new things in this JavaScript array tutorial.

Here is an Array called items holding bunch of numbers
const items = [1, 2, 3, 4]
We can also take strings instead of numbers
const items = ['Apple', 'Orange', 'Peach', 'Grapes']

I can access elements in this array by using index number.
Array index starts with zero.
I will type items zero to access first element in array.

items[0]
items[1]
items[2]
items[3]

What if I want to access last element in array?
For that I will need to know length of Array (array length is number of array elements).
For that I would type items.length -1.
This would give me last item in Array.

items[items.length - 1]
Access second last element
items[items.length - 2]
This would give me 3.
items[items.length - 3]
items[items.length - 4]

To access elements from left to right we can also use at method.

items.at(0)
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.
items.at(1)
items.at(2)
items.at(3)

We can access elements from end without calculating length of Array by using at method. This is called Relative Indexing Method that is new feature in JavaScript. Relative indexing via negative indices is a feature that enjoys popularity in other languages like Python.

items.at(-1)
-1 means the last element
items.at(-2)
items.at(-3)
items.at(-4)

So Array at method simplifies things a little bit where we do not have to find length of Array to access elements from the end.

✅ How To Hide / Protect JavaScript Code - Techniques and Tools
https://youtu.be/8VyzRlxo2-k
✅ React JS UNDEFINED Solution | Reactjs Learning | Learn ReactJS & JavaScript Debugging Fast
https://youtu.be/0IvxCxcEBPo
✅ Calculate Sum or Total from Values in Array & Array Object | Learn JavaScript | Reduce Method
https://youtu.be/Xw7G2b80dDg
✅ Get URL Parameters in JavaScript | URLSearchParams
https://youtu.be/CZP1iQFQjEY
✅ DIV to PDF, Iframe to PDF using JavaScript Only (with CSS & Images Support)
https://youtu.be/DV2tAG5E-F0
✅ How To UnMinify JavaScript Minified Files Easily | Decrypt Complex JavaScript Code
https://youtu.be/uh8_Rt4Qam8
✅ Toggle Class in Vanilla JavaScript | Learn JavaScript
https://youtu.be/IdtwowBlfmI
✅ How to Execute a JavaScript File by using NPM
https://youtu.be/2xcoenAdSWs
✅ Get User's Location (Country, State, City) with Free Unlimited API Access | PHP & JavaScript
https://youtu.be/_P8_JUaoNgs
✅ Replace jQuery with Vanilla JavaScript Code Easily | jQuery vs JavaScript
https://youtu.be/toJ3vRUMf2g
✅ Toggle in Vanilla JavaScript
https://youtu.be/ebDCyf-JFOw
✅ MINIFY JAVASCRIPT FILES & FOLDERS in Multiple Ways (Command Line & Code Editor)
https://youtu.be/umVoV4ak0HA
✅ JavaScript vs jQuery | Difference between JavaScript and jQuery
https://youtu.be/PrBSuz7rPlc
✅ JavaScript Variable Redeclare & Reassign | VAR vs LET vs CONST | Practical Examples
https://youtu.be/9NNva3x0awQ

Thank You!
👍 LIKE VIDEO
👊 SUBSCRIBE
🔔 PRESS BELL ICON
✍️ COMMENT

Channel: https://www.youtube.com/webstylepress
Website: https://www.webstylepress.com
FaceBook: https://www.facebook.com/webstylepress
Twitter: https://twitter.com/webstylepress
GitHub: https://github.com/webstylepress
#JavaScript #JS #Array #ArrayMethod #ArrayAt #JavaScriptArray #ArrayMethods #WebStylePress #webdevelopment

Тэги:

#Access_array_elements_from_any_side_in_javascript #javascript_tutorial_for_beginners #javascript #js #javascript_tutorial #js_tutorial #javascript_array #javascript_array_at #at_method #array_at_method #array_length #array_javascript #access_elements_in_array #array_negative #relative_indexing_method #javascript_arrays #array #javascript_array_methods #javascript_array_tutorial #learn_javascript #array_in_javascript #arrays_in_javascript #javascript_arrays_tutorial #Arrays
Ссылки и html тэги не поддерживаются


Комментарии: