北极熊是什么颜色的| 吃小龙虾不能和什么一起吃| 拔凉拔凉是什么意思| 七夕节的含义是什么| 碘是什么| 星期三左眼皮跳是什么预兆| 增强ct是什么意思| 筑基是什么意思| 头晕头疼是什么原因| 9月25日是什么星座| 肺结核复发有什么症状| 感冒了可以吃什么水果| 转氨酶高有什么危害| 血常规检查挂什么科| 纤支镜主要检查什么| 羊水破了是什么感觉| 胃胀胃痛吃什么药| 什么是天珠| 学无止境是什么意思| 什么样的人容易高反| 算了是什么意思| 烂嘴角是什么原因| 是什么样的感觉我不懂是什么歌| 贫血要做什么检查| 睡莲什么时候开花| 跃然纸什么| 什么水果可以泡酒| 后背有痣代表什么| 免疫球蛋白是什么| 猫代表什么数字| ais是什么意思| 1015是什么星座| 什么补钙效果最好| 九寨沟在什么地方| 朱砂有什么作用| 血儿茶酚胺是查什么的| 工科和理科有什么区别| 断码是什么意思| 头痛吃什么| negative什么意思| 梦见粽子是什么预兆| 洗发水和洗发露有什么区别| 背靠背是什么意思| 怀孕可以吃什么水果| 1882年属什么生肖| 入职体检70元一般检查什么| 补肝血吃什么食物最好| wrangler是什么牌子| 69年属什么生肖| 缺金的人戴什么最旺| 梅花开在什么季节| 侃侃而谈是什么意思| 什么是基因突变| 银针茶属于什么茶| 结痂什么意思| 生不逢时是什么意思| 一九八六年属什么生肖| 新生儿老打嗝什么原因| 晚上睡不着是什么原因引起的| 辽源有什么好玩的地方| 亚麻籽吃了有什么好处| 办健康证挂什么科| 什么是虚无主义| 金银花不能和什么一起吃| 荷花像什么| 为什么叫清明上河图| 一月五日是什么星座| bm是什么意思| 女性阴毛变白是什么原因| 孕妇喝什么水比较好| 支那人什么意思| bni是什么意思| 唯美什么意思| 脂肪肝是什么原因造成的| 六月是什么生肖| 梦见很多肉是什么意思| 乳头很痒是什么原因| 州字五行属什么| 月桂酰两性基乙酸钠是什么| 梦到被蛇咬是什么意思周公解梦| 总是睡不着觉是什么原因| 12月初是什么星座| 合疗和医保有什么区别| 开天辟地是什么生肖| oof是什么意思| 发福了是什么意思| 死有余辜什么意思| 甲钴胺片是治什么病| 趋是什么意思| 薰衣草什么时候开花| 钥字五行属什么| 肺部感染挂什么科| 为什么头皮会疼| 白矾是什么东西| 3的倒数是什么| 女性体毛多是什么原因| 副镇长是什么级别| 人生价值是什么| 6月16什么星座| 佑字五行属什么| 真菌感染是什么引起的| 双向转诊是什么意思| 大咖是什么意思| 芥子是什么意思| 扭转乾坤是什么生肖| 8月17号是什么日子| 早上6点是什么时辰| 明前茶什么意思| 鹿皮绒是什么面料| 胆囊息肉是什么原因造成的| 1994属什么| 献血之后吃什么比较好| 双顶径是指什么| 孩子反复发烧是什么原因| 小孩便秘吃什么最管用| 冰冻三尺非一日之寒什么意思| 开光的手串有什么禁忌| 伸张正义是什么意思| 什么是结缔组织病| 什么是体外受精| 瘘管是什么症状| 瑗字五行属什么| jk制服什么意思| 小拇指和无名指发麻是什么原因| 440分能上什么大学| 无花果和什么不能一起吃| 肝内低密度影是什么意思| 纷呈是什么意思| 珠联璧合是什么意思| 早上起来嘴巴苦是什么原因| 要强的女人是什么性格| 消肿用什么药| 天伦之乐是什么意思| 哈工大全称是什么| 反式脂肪是什么意思| mmc什么意思| 电母是什么意思| 咬牙齿是什么原因| 气血两亏是什么意思| 女人心肌缺血吃什么药| 棚户区改造和拆迁有什么区别| 鸡蛋和什么食物相克| 电动车电池什么牌子好| 祛斑喝什么花茶最有效| 回执单是什么| 桦树茸泡水喝有什么功效| 小肠火吃什么药| 金针菇不能和什么一起吃| 近水楼台先得月是什么生肖| 血小板低吃什么药| 臣附议是什么意思| 5月15日什么星座| 什么食物防辐射| 鼻干眼干口干属于什么症状| 长征是什么意思| rock什么意思| 莫非的近义词是什么| 孕妇梦见下雪是什么征兆| 体外射精是什么| 小米手机最新款是什么型号| cst是什么意思| na是什么牌子| 抛锚是什么意思| 倪妮和倪萍什么关系| 做完胃肠镜后可以吃什么| 朱元璋为什么要杀刘伯温| 喜欢紫色代表什么| 什么伐桂| 珙桐是什么植物| 6月1日是什么星座| 拔完牙能吃什么| 吃什么去黄褐斑最有效| 蜗牛吃什么| 什么是行政处罚| 幽门螺旋杆菌什么症状| 肚子左下方是什么器官| 手肿是什么原因引起的| 胎儿畸形是什么原因造成的| 附带是什么意思| 肥皂剧是什么意思| 前列腺钙化是什么原因引起的| 儿童感冒挂什么科| 阳历7月15日是什么星座| 丑人多作怪什么意思| 碎花裙配什么鞋子| 泻火是什么意思| 耳朵嗡嗡响是什么原因| 血滴子是什么意思| 医学cr是什么意思| 老年人缺钾是什么原因引起的| 痰培养是检查什么的| 汗为什么是咸的| p是什么面料| 什么是传染病| 早上起来后背疼是什么原因| 姥姥的妈妈叫什么| 配裙子穿什么鞋子好看| 狼烟是什么意思| 消化不良用什么药| 恶对什么| et是什么意思| 吃饺子是什么节日| n标志的鞋子是什么牌子| vans属于什么档次| 盐酸安罗替尼胶囊主要治疗什么| 梦到很多蛇是什么意思| 甲状腺功能减退是什么意思| 2013年是什么年| 晚上剪指甲有什么说法| 什么快递可以寄活物| 人死后为什么要守夜| ins风格是什么| 粉皮是什么做的| 睾丸扭转有什么症状| 吃什么补肾壮阳最快速| 下作是什么意思| 最大的荔枝是什么品种| 脂肪瘤是什么引起的| 支付宝账户是什么| 什么是干冰| 胃痛胃胀什么原因引起的| 什么叫无氧运动| 子宫在肚脐眼什么位置| 经常流鼻血是什么原因| 6月23号是什么日子| 为什么每天晚上睡觉都做梦| 褒义词是什么意思| 女生喝什么茶对身体好| 火把节在每年农历的什么时间举行| 正月十二是什么星座| 火碱是什么东西| 邓字五行属什么| 手上的月牙代表什么意思| 救济的近义词是什么| 唯有读书高的前一句是什么| 变化无穷是什么生肖| 得水痘不能吃什么| 升白细胞的针剂叫什么| 三什么一什么| 未加一笔是什么字| 鼠是什么命| 笃行是什么意思| 为什么老是放屁| 11月8日什么星座| 霉菌有什么症状| 10月16日是什么星座| 酒量越来越差什么原因| 有点拉肚子吃什么药| 甘胆酸是查什么的| 外面下着雨犹如我心血在滴什么歌| 盗墓笔记讲的是什么故事| 为什么会扁桃体发炎| 脑脊液白细胞高是什么原因| 98年出生属什么| 科学的尽头是什么| 腹股沟疝气挂什么科| 住院带什么必备物品| 为什么怀不上孩子| 核磁和ct有什么区别| 金牛和什么星座最配| 从此萧郎是路人是什么意思| 文笔是什么意思| 难为情是什么意思| 高血糖适合吃什么水果| 百度
Table of Contents
1. Accessing the First Element
2. Accessing the Last Element
Alternative Methods (Modern JavaScript)
Using .at() method (ES2022 )
Using destructuring and rest (for the first)
Summary
Home Web Front-end JS Tutorial How can you access the first and last elements of a JavaScript array?

美媒:中国前两月工业与投资增速加快 经济显露融融春意

Aug 04, 2025 am 04:17 AM
array

百度 五、注重智慧城市的建设,并统一建设标准,实现在综合平台上互联互通。

  1. The first element is accessed using arr[0] or arr.at(0). 2. The last element is accessed using arr[arr.length - 1] or arr.at(-1), with .at(-1) being more readable in modern JavaScript, and both return undefined for empty arrays.

How can you access the first and last elements of a JavaScript array?

You can access the first and last elements of a JavaScript array using simple indexing and built-in properties. Here's how:

How can you access the first and last elements of a JavaScript array?

1. Accessing the First Element

The first element of an array is always at index 0. You can access it directly using bracket notation:

const arr = ['apple', 'banana', 'cherry'];
const first = arr[0];
console.log(first); // 'apple'

?? If the array is empty, arr[0] will return undefined.

How can you access the first and last elements of a JavaScript array?

2. Accessing the Last Element

Since array lengths can vary, the last element is at index arr.length - 1. Use this formula to get it:

const arr = ['apple', 'banana', 'cherry'];
const last = arr[arr.length - 1];
console.log(last); // 'cherry'

? This works reliably as long as the array has at least one element. For empty arrays, it returns undefined.

How can you access the first and last elements of a JavaScript array?

Alternative Methods (Modern JavaScript)

Using .at() method (ES2022 )

The .at() method allows relative indexing. Use at(-1) for the last element:

const arr = ['apple', 'banana', 'cherry'];
console.log(arr.at(0));  // 'apple' — first
console.log(arr.at(-1)); // 'cherry' — last

This is cleaner and more readable, especially for the last element.

Using destructuring and rest (for the first)

You can extract the first element via destructuring:

const [first] = ['apple', 'banana', 'cherry'];
console.log(first); // 'apple'

And for the last, it's less practical but possible with reverse or length tricks — not recommended for this purpose.


Summary

  • First element: arr[0] or arr.at(0)
  • Last element: arr[arr.length - 1] or arr.at(-1)

Use .at(-1) if you're targeting modern environments — it's more expressive and avoids the length - 1 math.

Basically, it's straightforward — just remember the index rules and watch out for empty arrays.

The above is the detailed content of How can you access the first and last elements of a JavaScript array?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ??takes a relatively long time.

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

PHP array multi-dimensional sorting practice: from simple to complex scenarios PHP array multi-dimensional sorting practice: from simple to complex scenarios Apr 29, 2024 pm 09:12 PM

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Apr 30, 2024 pm 03:42 PM

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

Can arrays be used as function parameters? Can arrays be used as function parameters? Jun 04, 2024 pm 04:30 PM

Yes, in many programming languages, arrays can be used as function parameters, and the function will perform operations on the data stored in it. For example, the printArray function in C++ can print the elements in an array, while the printArray function in Python can traverse the array and print its elements. Modifications made to the array by these functions are also reflected in the original array in the calling function.

How to sort array keys according to their length in PHP and keep the keys? How to sort array keys according to their length in PHP and keep the keys? May 02, 2024 pm 01:03 PM

Through the uksort() function and the custom comparison function compareKeyLengths, PHP arrays can be sorted according to the length of the array key names while retaining the key names. The comparison function calculates the difference in key lengths and returns an integer, and uksort() sorts the array according to this integer. In addition, a practical case demonstrates how to sort records from the database by field name length.

See all articles
三焦指的是什么 生意盎然什么意思 舌苔厚黄吃什么药最好 充电宝什么品牌好 竹鼠吃什么
1966年是什么命 揶揄什么意思 海茸是什么东西 买单是什么意思 什么药吃了会产生幻觉
中医心脉受损什么意思 贫血吃什么补得快 左侧卵巢内囊性回声是什么意思 梦见下大雨是什么预兆 陈皮有什么功效
色散是什么意思 什么叫椎间盘膨出 税号是什么 pa什么意思 三个又是什么字
儿童湿疹用什么药hcv7jop6ns3r.cn 脑干堵塞什么症状yanzhenzixun.com 木姜子是什么东西hcv8jop3ns8r.cn 有痰是什么原因hcv8jop3ns2r.cn 清心寡欲下一句是什么hcv8jop4ns4r.cn
小孩晚上睡觉出汗是什么原因hcv9jop6ns2r.cn 带蜜蜡有什么好处hcv9jop0ns5r.cn 电测听是什么hcv9jop6ns9r.cn 铁娘子是什么意思hcv7jop9ns1r.cn 三月份是什么星座的hcv8jop9ns3r.cn
什么泡面最好吃hcv7jop5ns3r.cn 白天嗜睡是什么原因hcv7jop5ns1r.cn 情妇是什么意思helloaicloud.com 番茄是什么时候传入中国的shenchushe.com 急性痛风吃什么药hcv8jop4ns0r.cn
钱丢了预示着什么hcv9jop6ns5r.cn 超敏c反应蛋白偏高说明什么hcv7jop9ns4r.cn 北极为什么没有企鹅hcv9jop6ns7r.cn 心口下面疼是什么原因hcv8jop4ns8r.cn 灰色裤子配什么上衣hcv9jop2ns4r.cn
百度