JavaScript pop()メソッドを使って配列から最後の要素を取り除くサンプル
環境
Windows 10 home 64bit
Google Chrome 107.0.5304.122
構文
const 配列名 =[要素1,要素2,…
配列名.pop()
pop() メソッドは、配列から最後の要素を取り除き、その要素を返します。このメソッドは配列の長さを変化させます。
使用例
const city = ['study', 'skill', 'become', 'smart', 'arkgame'];
console.log("最後の要素: "+city.pop());
console.log("結果1: "+city);
city.pop();
console.log("結果2: "+city);
const city = ['study', 'skill', 'become', 'smart', 'arkgame'];
console.log("最後の要素: "+city.pop());
console.log("結果1: "+city);
city.pop();
console.log("結果2: "+city);
const city = ['study', 'skill', 'become', 'smart', 'arkgame']; console.log("最後の要素: "+city.pop()); console.log("結果1: "+city); city.pop(); console.log("結果2: "+city);
実行結果
> “最後の要素: arkgame"
> “結果1: study,skill,become,smart"
> “結果2: study,skill,become"