
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Awkward behaviour of delete operator on arrays in JavaScript
The delete operator in JavaScript is actually an object operator (used with objects).
But since arrays are also indexed objects in JavaScript, we can use the delete operator with arrays as well.
Consider the following array of literals −
const arr = ['a', 'b', 'c', 'd', 'e'];
Example
Let us now execute the following program and guess the expected output −
const arr = ['a', 'b', 'c', 'd', 'e']; delete arr[4]; console.log(arr); console.log(arr.length);
Output
The output of this program in the console will be −
[ 'a', 'b', 'c', 'd', <1 empty item> ] 5
Understanding the output −
Since we deleted one index value of the array, we expected the array.length to output 4 instead of 5. But the delete operator only removes the value from the memory location and the location is still occupied by the array.
This makes no change to the length of the array and we still see 5 elements in the array just one memory location is empty now.
- Related Articles
- Spread operator for arrays in JavaScript
- How to delete a getter using the delete operator in JavaScript?
- How to delete a setter using the delete operator in JavaScript?
- How to work with delete operator in JavaScript?
- What is 'delete' Operator in JavaScript?
- delete() operator in C++
- JavaScript merge multiple Boolean arrays with the OR || operator
- new and delete operator in C++
- How to use delete Operator in TypeScript?
- How to use spread operator to join two or more arrays in JavaScript?
- Finding content of arrays on the basis of specific property in JavaScript?
- Delete duplicate elements based on first letter – JavaScript
- JavaScript : Why does % operator work on strings? - (Type Coercion)
- Aging Process and Its Impact on Consumer Behaviour
- AND product of arrays in JavaScript

Advertisements