JavaScript: Empty an array keeping the original
JavaScript Array: Exercise-33 with Solution
Empty Array
Write a JavaScript script to empty an array while keeping the original.
Sample Solution:
JavaScript Code:
Sample Output:
[1,3,6,3,-5] []
ES6 Version:
Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript function that empties an array by setting its length property to zero while preserving the original reference.
- Write a JavaScript function that returns a new empty array without modifying the original array.
- Write a JavaScript function that uses a loop to remove elements one by one using pop().
- Write a JavaScript function that clears an array and confirms that subsequent operations are performed on an empty array.
Improve this sample solution and post your code through Disqus.
Previous: Write a JavaScript function to find an array contains a specific element.
Next: Write a JavaScript function to get nth largest element from an unsorted array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics