Empty Array In AngularJs JavaScript

Empty Array In AngularJs JavaScript – We often need to clear array while working with arrays in AngularJs. It is very simple to clear array in AngularJs. You can just initialize the array to clear it. Here in this tutorial we are going to explain how to you can clear array items in AngularJs. You can also use our online editor to edit and run the code online.

How to Empty Array In AngularJs JavaScript | Clear Array | Remove All Items Of Array

If you want to remove all items of array in AngularJs just initialize the array as below –

Clear Array Items In AngularJs : Example

<!DOCTYPE html>
<html lang="en">
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
$scope.exampleArray = ['Mango','Banana'];
 $scope.clearArray = function() {
	$scope.exampleArray = [];
<div ng-app="myApp">  
<div ng-controller="myController"> 
 Click to clear Array .
 <button ng-click='clearArray()' >Clear Array</button><br>
 Array = {{exampleArray}}<br>


Try it »

If you click on the clear button it will clear the array exampleArray and it will initialize the array again.

If you run the above example it will produce output something like this –

Empty Array In AngularJs JavaScript


Add Comment

📖 Read More