Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Interesting Facts about Object in JavaScript
Next article icon

Interesting Facts about JavaScript Arrays

Last Updated : 10 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Let us talk about some interesting facts about JavaScript Arrays that can make you an efficient programmer.

Arrays are Objects

JavaScript arrays are actually specialized objects, with indexed keys and special properties. They have a length property and are technically instances of the Array constructor.

JavaScript
const a = [10, 20, 30]; console.log(typeof a); 

Output
object 

You can add non-integer properties to arrays, making them work partly like objects. However, this is usually discouraged for clarity.

JavaScript
let a = [1, 2, 3];  // Adding a property to array (NOT RECOMMEMDED IN PRACTICE) a.name = "MyArray";  console.log(a.name);         // Iterating through the array with `for...in` // (NOT RECOMMENDED IN PRACTICE) for (let key in a) {   console.log(`${key}: ${a[key]}`); } 

Output
MyArray 0: 1 1: 2 2: 3 name: MyArray 

Mixed Elements Allowed

Like Python and unlike C/C++/Java,, we can have mixed type of elements in a JavaScript array.

JavaScript
const a = [10, "hi", true]; console.log(a); 

Output
[ 10, 'hi', true ] 

Dynamic Size

Like Python and unlike C/C++/Java,, the default array implementation is Dynamic Size.

JavaScript
let a = [1, 2, 3]; a.push(4);  console.log(a); 

Output
[ 1, 2, 3, 4 ] 

Negative Indexing

The .at() method, introduced in ES2022, allows access to array elements using negative indices, making it easy to retrieve elements from the end of an array without calculating the length.

JavaScript
const arr = [10, 20, 30]; console.log(arr.at(-1));  

Output
30 

Resizing an Array

We can resize a JavaScript array by simply changing its length property.

JavaScript
let a = [1, 2, 3, 4]; a.length = 2; console.log(a);  

Output
[ 1, 2 ] 


Array Assignment

When we assign an array to another, it only creates one more reference to the same array.

JavaScript
// changed the original array let a = [10, 20]; let b = a;  b.push(30);  console.log(a);  

Output
[ 10, 20, 30 ] 

Spread Operator

We get members of an array or a string. It helps us in copying in copying an array, concatenating arrays and passing array elements to different parameters of a function.

JavaScript
// Arrat copy using spread operator let a = [10, 20]; let b = [...a];  b.push(30);  console.log(a);  

Output
[ 10, 20 ] 
JavaScript
// Array concatenation using spread operator let a = [10, 20]; let b = [30, 40]; let c = [...a, ...b] console.log(c);  

Output
[ 10, 20, 30, 40 ] 
JavaScript
function add(x, y, z) {   return x + y + z; }  let a = [10, 20, 30]; console.log(add(...a)); 

Output
60 

Empty Elements in Array

JavaScript allows empty elements in an array. When we access these elements, we get undefined.

JavaScript
const a = [1, , , 3];  console.log(a);  console.log(a[1]); 

Output
[ 1, <2 empty items>, 3 ] undefined 
JavaScript
const a = [1, 3];  a.length = 4 console.log(a);  console.log(a[2]); 

Output
[ 1, 3, <2 empty items> ] undefined 

Direct Methods to Modify Arrays

JavaScript allows multiple direct methods for efficient programming.

JavaScript
const a = [1, 2, 3, 4];  const b = a.map(x => x * 2);  console.log(b)  const c = a.filter(x => x > 2);  console.log(c)  const d = [1, [2, 3], [4, [5]]]; console.log(d.flat(2));  

Output
[ 2, 4, 6, 8 ] [ 3, 4 ] [ 1, 2, 3, 4, 5 ] 



Next Article
Interesting Facts about Object in JavaScript
author
kartik
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-array

Similar Reads

  • Interesting Facts About JavaScript Data Types
    JavaScript (often abbreviated as JS) is one of the most popular programming languages in the world. It comes with its unique take on data types that sets it apart from languages like C, C++, Java, or Python. Understanding how JavaScript handles data types will be very interesting, which can help you
    6 min read
  • Interesting Facts about Object in JavaScript
    Let's see some interesting facts about JavaScript Objects that can help you become an efficient programmer. JavaSctipt Objects internally uses Hashing that makes time complexities of operations like search, insert and delete constant or O(1) on average. It is useful for operations like counting freq
    4 min read
  • JavaScript - Insert Element in an array
    In JavaScript elements can be inserted at the beginning, end, and at any specific index. JS provides several methods to perform the operations. At the Beginning This operation inserts an element at the start of the array. The unshift() method is commonly used, which mutates the original array and re
    2 min read
  • What are Associative Arrays in JavaScript ?
    Associative arrays in JavaScript, commonly referred to as objects, are crucial for storing key-value pairs. This guide explores the concept and usage of associative arrays, providing insights into their benefits and applications. Example: // Creating an associative array (object)let arr= { name: "Ge
    2 min read
  • JavaScript - Iterate Over an Array
    JavaScript for Loop can be used to iterate over an array. The for loop runs for the length of the array and in each iteration it executes the code defined inside. We can access the array elements using the index number. 1. Using for...of LoopThe for…of loop iterates over the values of an iterable ob
    3 min read
  • JavaScript Array find() function
    JavaScript arr.find() function is used to find the first element from the array that satisfies the condition implemented by a function. If more than one element satisfies the condition then the first element satisfying the condition is returned. Syntax: arr.find(function(element, index, array), this
    3 min read
  • JavaScript Array() Constructor
    The Array() constructor is used to create Array objects and the array constructor can be called with or without a new keyword, both can create a new Array. Syntax:new Array(Value1, Value2, ...);new Array(ArrayLength);Array(Value1, Value2, ...);Array(ArrayLength);Parameters: ValueN: An array initiali
    2 min read
  • JavaScript Array Iteration Methods
    JavaScript Array iteration methods perform some operation on each element of an array. Array iteration means accessing each element of an array. There are some examples of Array iteration methods are given below: Using Array forEach() MethodUsing Array some() MethodUsing Array map() MethodMethod 1:
    3 min read
  • Copy Array Items Into Another Array in JavaScript
    In JavaScript, copying array items into another array in JavaScript is helpful when you want to make changes to the data without affecting the original array. It helps avoid mistakes, keeps the original data safe, and is useful when you need to work with a separate copy of the array for testing or p
    5 min read
  • Fastest way to duplicate an array in JavaScript
    Multiple methods can be used to duplicate an array in JavaScript.The fastest way to duplicate an array in JavaScript is by using the slice() Method. Let us discuss some methods and then compare the speed of execution. The methods to copy an array are: Table of Content Using slice() Using concat() me
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences