pop()

2019-12-29  本文已影响0人  jeanzy

定义和用法

pop() 方法用于删除并返回数组最后一个元素

语法

arrayObject.pop()

返回值

arrayObject 的最后一个元素。

说明

pop() 方法将删除 arrayObject 的最后一个元素,把数组长度减 1,并且返回删除的元素

如果数组已经为空,则 pop() 不改变数组,并返回 undefined 值。


实例

在本例中,我们将创建一个数组,然后删除数组的最后一个元素。请注意,这也会改变数组的程度:

var arr = new Array(3)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr)document.write("
")document.write(arr.pop())document.write("
")document.write(arr)var arr = new Array(3)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr)document.write("
")document.write(arr.pop())document.write("
")document.write(arr)var arr = new Array(3)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr)document.write("
")document.write(arr.pop())document.write("
")document.write(arr)var arr = new Array(3)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr)document.write("
")document.write(arr.pop())document.write("
")document.write(arr)var arr = new Array(3)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr)document.write("
")document.write(arr.pop())document.write("
")document.write(arr)var arr = new Array(3)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr)document.write("
")document.write(arr.pop())document.write("
")document.write(arr)<script type="text/javascript">

        var arr = new Array(3)

        arr[0] = "George"

        arr[1] = "John"

        arr[2] = "Thomas"

        document.write(arr)

        document.write("<br />")

        document.write(arr.pop())

        document.write("<br />")

        document.write(arr)

</script>

var arr = new Array(3)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr)document.write("
")document.write(arr.pop())document.write("
")document.write(arr)

输出:

George,John,Thomas

Thomas

George,John

上一篇下一篇

猜你喜欢

热点阅读