ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# js中push pop unshift shift的区别 ``` <pre class="calibre29">``` <span class="token5">1</span><span class="token3">、push</span><span class="token1">(</span><span class="token1">)</span><span class="token3">、pop</span><span class="token1">(</span><span class="token1">)</span><span class="token3">和unshift</span><span class="token1">(</span><span class="token1">)</span><span class="token3">、shift</span><span class="token1">(</span><span class="token1">)</span>   这两组同为对数组的操作,并且会改变数组的本身的长度及内容。 <span class="token3">  不同的是 push</span><span class="token1">(</span><span class="token1">)</span><span class="token3">、pop</span><span class="token1">(</span><span class="token1">)</span> <span class="token3">是从数组的尾部进行增减,unshift</span><span class="token1">(</span><span class="token1">)</span><span class="token3">、shift</span><span class="token1">(</span><span class="token1">)</span> 是从数组的头部进行增减。 <span class="token2">var</span> <span><span class="token4">`arr = [1,2];`</span></span> <span class="token5">2</span><span class="token3">、push</span><span class="token1">(</span><span class="token1">)</span><span class="token3">和unshift</span><span class="token1">(</span><span class="token1">)</span>   向数组的 尾部<span class="token">/</span>头部 <span class="token">**</span><span class="token">*</span>添加若干元素<span class="token">**</span><span class="token">*</span>,并返回 数组的 <span class="token">**</span>新长度<span class="token">**</span>;   arr<span class="token1">.</span><span class="token3">push</span><span class="token1">(</span><span class="token5">3</span><span class="token1">,</span><span class="token5">4</span><span class="token1">)</span>; <span class="token6">//返回 arr 的新长度 4</span>   arr <span class="token1">;</span> <span class="token6">// arr = \[1,2,3,4\];</span>   arr<span class="token1">.</span><span class="token3">unshift</span><span class="token1">(</span><span class="token5">0</span><span class="token1">,</span><span class="token5">0.5</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// 返回 arr 的新长度 6</span>   arr <span class="token1">;</span> <span class="token6">// arr = \[0,0.5,1,2,3,4\];</span> <span class="token5">3</span><span class="token3">、pop</span><span class="token1">(</span><span class="token1">)</span><span class="token3">和shift</span><span class="token1">(</span><span class="token1">)</span>   从数组的 尾部<span class="token">/</span>头部<span class="token">**</span><span class="token">*</span><span class="token3">删除1个元素</span><span class="token1">(</span>删且只删除<span class="token5">1</span>个<span class="token1">)</span><span class="token">**</span><span class="token">*</span>,并返回<span class="token">**</span>被删除的元素<span class="token">**</span>;空数组是继续删除,不报错,但返回undefined;   arr<span class="token1">.</span><span class="token3">pop</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span>      <span class="token6">//返回 4;</span>   arr <span class="token1">;</span>         <span class="token6">// arr = \[0,0.5,1,2,3\];</span>   arr<span class="token1">.</span><span class="token3">pop</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span>      <span class="token6">//返回 3;</span>   arr <span class="token1">;</span>         <span class="token6">// arr = \[0,0.5,1,2\];</span>   arr<span class="token1">.</span><span class="token3">shift</span><span class="token1">(</span><span class="token1">)</span>;     <span class="token6">// 返回 0 ;</span>   arr <span class="token1">;</span>        <span class="token6">// arr = \[0.5,1,2\]</span>   <span class="token">**</span><span class="token5">PS</span><span class="token1">:</span><span class="token">**</span><span class="token3"> pop</span><span class="token1">(</span><span class="token1">)</span><span class="token3">和shift</span><span class="token1">(</span><span class="token1">)</span> 不接受传参,即使传了参数也没什么卵用<span class="token">~</span><span class="token">~</span>;   arr<span class="token1">.</span><span class="token3">pop</span><span class="token1">(</span><span class="token5">3</span><span class="token1">)</span> <span class="token1">;</span> <span class="token6">// 返回 2;**永远返回最后一个;**</span>   arr <span class="token1">;</span>        <span class="token6">// arr = \[0.5,1\];</span>   arr<span class="token1">.</span><span class="token3">shift</span><span class="token1">(</span><span class="token5">1</span><span class="token1">)</span><span class="token1">;</span>    <span class="token6">// 返回 0.5; **永远返回第一个;**</span>   arr <span class="token1">;</span>        <span class="token6">// arr = \[1\];</span>   arr<span class="token1">.</span><span class="token3">pop</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">;</span>     <span class="token6">// 返回 1;</span>   arr <span class="token1">;</span>        <span class="token6">// arr = \[\];</span>   arr<span class="token1">.</span><span class="token3">shift</span><span class="token1">(</span><span class="token1">)</span>     <span class="token6">// 返回 undefined;</span> ``` ``` arr ; // arr = \[\];